2017-03-09 6 views
2

this answerで説明されているように、カスタムRxJS演算子を実装しようとしています。ここに私のアプリケーション内の関連するスニペットです:アングル2プロジェクトで宣言のマージが機能しない

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; 

consumer.ts

load()が呼び出され
import { Observable } from 'rxjs/Observable'; 
import '../../rxjs-extensions'; 
... 
export class MyComponent {  

... 

    private load(): void { 
     Observable.of(1).restrictToCommand(); 
    } 

... 

} 

が、私は次の例外を受け取る:

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を使用しています。

+0

TypeScript 2.2.1および2.0.3ではうまく動作します。その時点で 'Observable.prototype.restrictToCommand'が何であるかを正確に見るために、' load'にいくつかのログを追加したいかもしれません。 – cartant

+0

'console.log(Observable.of(1).restrictToCommand);' 'restrictToCommand'が定義されていないことを示しています... –

+0

複数のRxJSがインストールされていませんか?あなたのプロジェクトの構造を知らなければ、 '../../'が何を意味するのかは明確ではありません。 'rxjs-extensions.ts'のコードはうまく見えるので、' Observable'プロトタイプに 'restrictToCommand'が含まれていなければ、複数のRxJSモジュールが浮動していると思います。 'npm list rxjs'を実行して、複数のモジュールを見つけることができるかどうか確認してください。 – cartant

答えて

1

は、私は、私のapp.module.tsimport './rxjs-extensions'を追加する必要がありました私のアプリケーションで使用していました。

残念ながら、私はまだ何が起こっているか完全に理解していません。もし私が今までより明確な理解を得るなら、私は答えを更新します。

0

Observable.of演算子を追加しましたか?私はすでに./rxjs-imports、そのI RXJS演算子のすべてが含まれていたファイルをインポートしたところ、この問題を解決するには

あなたrxjs-拡張モジュールで

import 'rxjs/add/observable/of'; 
+0

'rxjs-imports'から私が使いたい演算子に加えて' of'をインポートします。これは 'app.module.ts'でインポートされます。これは、PluralsightビデオでRxJSバンドルサイズを最適化するための提案されたアプローチでした。 –

関連する問題