【发布时间】:2019-03-29 19:05:06
【问题描述】:
目标
我正在尝试将 rxjs 的 typescript 模块增强作为 npm 包提供给 Angular 项目。
问题
在 Angular 应用程序中使用包在本地开发模式下可以正常工作,但是一旦为生产构建了应用程序,包就不会被导入到最终的 dist 中。
详情
-
大部分增强是提供一种新方法:Observable.safeSubscribe()
(完整源代码:ngx-safe-subscribe.ts)import { Observable, Subscription } from 'rxjs'; declare module 'rxjs/internal/Observable' { interface Observable<T> { safeSubscribe: typeof safeSubscribe; } } export function safeSubscribe<T>(...): Subscription { ... } Observable.prototype.safeSubscribe = safeSubscribe; 包是用ng-packagr构建的
-
在 Angular 项目中导入并使用后,一切正常:
(在此处完成演示:demo)import { Component, OnDestroy } from '@angular/core'; import { of } from 'rxjs'; import '@badisi/ngx-safe-subscribe'; @Component({ selector: 'my-app', templateUrl: './app.component.html' }) export class AppComponent implements OnDestroy { constructor() { of([]).safeSubscribe(this, () => { console.log('ok'); }); } ngOnDestroy(): void {} } -
一旦为生产而构建,包就不会导入到最终的 dist 中:
ng serve --prod [error in console]: TypeError: Object(...)(...).safeSubscribe is not a function -
我最后的尝试是用最新的东西进行的
angular@7.0.0, typescript@3.1.3, ng-packagr@4.4.0 -
附带说明,在 Visual Studio Code 中使用命名的导入样式会产生以下结果:
import { safeSubscribe } from '@badisi/ngx-safe-subscribe'; [ts] 'safeSubscribe' is declared but its value is never read.
很难判断问题是否来自 typescript、angular-cli、webpack 甚至 ng-packagr 无法识别扩充并将其正确导入最终 dist。
因此,我们将非常感谢您的帮助!
谢谢。
【问题讨论】:
标签: typescript npm webpack angular-cli ng-packagr