声明合并在Angular 2项目中不起作用 [英] Declaration Merging not Working in Angular 2 Project

查看:75
本文介绍了声明合并在Angular 2项目中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自定义RxJS运算符,如此答案中所述.以下是我的应用程序中的相关片段:

I am trying to implement a custom RxJS operator as described in this answer. Here are the relevant snippets in my application:

rxjs-extensions.ts

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

consumer.ts

import { Observable } from 'rxjs/Observable';
import '../../rxjs-extensions';
...
export class MyComponent {    

...

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

...

}

调用 load()时,出现以下异常:

When load() is called, I receive the following exception:

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.

I am using Angular 2, Webpack 2, and TypeScript 2.0.3.

推荐答案

要解决此问题,我必须在 app.module中添加 import'./rxjs-extensions'.ts ,我已经在其中导入了 ./rxjs-imports ,该文件包含了我在应用程序中使用的所有RXJS运算符.

To fix this issue, I had to add import './rxjs-extensions' to my app.module.ts, where I was already importing ./rxjs-imports, a file that contained all of the RXJS operators that I was using in my application.

不幸的是,我仍然不完全了解正在发生的事情.如果我有更清楚的了解,我将更新答案.

Unfortunately, I still don't fully understand what is happening. I will update the answer if I ever gain a clearer understanding.

这篇关于声明合并在Angular 2项目中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆