Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
声明合并在Angular 2项目中不起作用_Angular_Typescript_Webpack 2 - Fatal编程技术网

声明合并在Angular 2项目中不起作用

声明合并在Angular 2项目中不起作用,angular,typescript,webpack-2,Angular,Typescript,Webpack 2,我正在尝试实现一个自定义RxJS操作符,如中所述。以下是我的应用程序中的相关代码片段: rxjs-extensions.ts import { Observable } from 'rxjs/Observable'; function restrictToCommand<T>(this: Observable<T>): Observable<T> { console.log('works'); return Observable.empty(

我正在尝试实现一个自定义RxJS操作符,如中所述。以下是我的应用程序中的相关代码片段:

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;
调用
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)
异常:未捕获(承诺中):错误:./MyParentComponent类MyParentComponent-内联模板中的错误:2:4由以下原因引起:_网页包_u导入的模块_u2_rxjs_uxJS_xJS_xJS_xJS_xObservable.Observable.of(…)。restrictToCommand不是函数
TypeError:uu网页包_u导入的_u模块_u2_xjs_u可观察的uuu可观察的。(…)。restrictToCommand不是函数
在MyComponent.load时(评估时(http://localhost:8080/app.js:3530:1), :45:75)
在MyComponent.ngOnInit(评估在(http://localhost:8080/app.js:3530:1), :63:14)
在Wrapper_MyComponent.ngDoCheck(/AppModule/JobsPanelComponent/Wrapper.ngfactory.js:22:53)
在CompiledTemplate.proxyViewClass.View\u MyParentComponent 0.detectChangesInternal(/AppModule/MyParentComponent/component.ngfactory.js:62:32)
在CompiledTemplate.proxyViewClass.AppView.detectChanges处(在(http://localhost:8080/app.js:3185:1), :301:14)
在CompiledTemplate.proxyViewClass.DebugAppView.detectChanges处(在(http://localhost:8080/app.js:3185:1), :394:44)
在CompiledTemplate.proxyViewClass.View_MyParentComponent_Host0.detectChangesInternal(/AppModule/MyParentComponent/host.ngfactory.js:29:19)
在CompiledTemplate.proxyViewClass.AppView.detectChanges处(在(http://localhost:8080/app.js:3185:1), :301:14)
在CompiledTemplate.proxyViewClass.DebugAppView.detectChanges处(在(http://localhost:8080/app.js:3185:1), :394:44)
在ViewRef时检测到的变化(在(http://localhost:8080/app.js:1708:1), :136:20)

我正在使用Angular 2、Webpack 2和TypeScript 2.0.3。

是否添加了Observable.of运算符

在rxjs扩展模块中

import 'rxjs/add/observable/of';

为了解决这个问题,我必须将
import./rxjs extensions'
添加到我的
app.module.ts
,在那里我已经导入了
/rxjs imports
,一个包含我在应用程序中使用的所有rxjs操作符的文件


不幸的是,我仍然不能完全理解正在发生的事情。如果我有更清楚的理解,我会更新答案。

我可以使用TypeScript 2.2.1和2.0.3。您可能希望向
load
添加一些日志记录,以准确查看
可观察的.prototype.restrictToCommand
此时是什么向我显示,
restrictToCommand
未定义…您确定没有多个RxJS安装吗?在不了解项目结构的情况下,不清楚
。/../
到底需要什么。
rxjs extensions.ts
中的代码看起来很好,因此如果导入后
Observable
原型不包含
restrictToCommand
,我猜会有多个rxjs模块浮动。运行
npm list rxjs
查看它是否能够找到多个模块到我的
app.module.ts
文件,我已经在其中导入了
/rxjs imports
。这似乎已经解决了这个问题!现在要理解为什么……:)一旦你弄清楚发生了什么,请在
rxjs imports
中发布一个.I import
of
,以及我想使用的任何其他操作符,然后在
app.module.ts
中导入。这是Pluralsight视频中建议的优化RxJS捆绑包大小的方法。
import 'rxjs/add/observable/of';