我正在尝试实现自定义 RxJS 运算符,如 this answer 中所述.以下是我的应用程序中的相关片段:
rxjs-extensions.ts
import { Observable } from 'rxjs/Observable';
function restrictToCommand<T>(this: Observable<T>): Observable<T> {
console.log('works');
return Observable.empty()
}
declare module 'rxjs/Observable' {
interface Observable<T> {
restrictToCommand: typeof restrictToCommand;
}
}
Observable.prototype.restrictToCommand = restrictToCommand;
消费者.ts
import { Observable } from 'rxjs/Observable';
import '../../rxjs-extensions';
...
export class MyComponent {
...
private load(): void {
Observable.of(1).restrictToCommand();
}
...
}
当 load()
被调用时,我收到以下异常:
EXCEPTION: Uncaught (in promise): Error: Error in ./MyParentComponent class MyParentComponent - inline template:2:4 caused by: __WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.of(...).restrictToCommand is not a function
TypeError: __WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.of(...).restrictToCommand is not a function
at MyComponent .load (eval at <anonymous> (http://localhost:8080/app.js:3530:1), <anonymous>:45:75)
at MyComponent .ngOnInit (eval at <anonymous> (http://localhost:8080/app.js:3530:1), <anonymous>:63:14)
at Wrapper_MyComponent .ngDoCheck (/AppModule/JobsPanelComponent/wrapper.ngfactory.js:22:53)
at CompiledTemplate.proxyViewClass.View_MyParentComponent 0.detectChangesInternal (/AppModule/MyParentComponent/component.ngfactory.js:62:32)
at CompiledTemplate.proxyViewClass.AppView.detectChanges (eval at <anonymous> (http://localhost:8080/app.js:3185:1), <anonymous>:301:14)
at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (eval at <anonymous> (http://localhost:8080/app.js:3185:1), <anonymous>:394:44)
at CompiledTemplate.proxyViewClass.View_MyParentComponentt_Host0.detectChangesInternal (/AppModule/MyParentComponent/host.ngfactory.js:29:19)
at CompiledTemplate.proxyViewClass.AppView.detectChanges (eval at <anonymous> (http://localhost:8080/app.js:3185:1), <anonymous>:301:14)
at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (eval at <anonymous> (http://localhost:8080/app.js:3185:1), <anonymous>:394:44)
at ViewRef_.detectChanges (eval at <anonymous> (http://localhost:8080/app.js:1708:1), <anonymous>:136:20)
我正在使用 Angular 2、Webpack 2 和 TypeScript 2.0.3。
最佳答案
为了解决这个问题,我必须将 import './rxjs-extensions'
添加到我的 app.module.ts
中,我已经在其中导入了 ./rxjs-imports
,一个包含我在我的应用程序中使用的所有 RXJS 运算符的文件。
不幸的是,我仍然不完全理解发生了什么。如果我有更清晰的理解,我会更新答案。
关于angular - 声明合并在 Angular 2 项目中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42706795/