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

编程入门 行业动态 更新时间:2024-10-27 00:34:21
本文介绍了声明合并在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> (localhost:8080/app.js:3530:1), <anonymous>:45:75) at MyComponent .ngOnInit (eval at <anonymous> (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> (localhost:8080/app.js:3185:1), <anonymous>:301:14) at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (eval at <anonymous> (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> (localhost:8080/app.js:3185:1), <anonymous>:301:14) at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (eval at <anonymous> (localhost:8080/app.js:3185:1), <anonymous>:394:44) at ViewRef_.detectChanges (eval at <anonymous> (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项目中不起作用

本文发布于:2023-11-26 17:37:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634533.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   中不   声明   项目   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!