2017-08-27 92 views
0

我正在用angular-cli 1.2.6构建一个小型的Angular 4应用程序。在开发过程中一切正常,并且它使用ng build --dist成功构建。但在运行时,第三方供应商之一toastr我用作服务没有找到 - 当注入时它是undefined。这是我如何包含到项目:Angular 4 with angular-cli - 找不到AOT的第三方提供商

.angular-cli.json

... 

"scripts": [ 
    ... 
    "../node_modules/toastr/build/toastr.min.js" 
    ], 

... 

我已经在一个单独的文件中创建一个注射令牌:

import {InjectionToken} from '@angular/core'; 
import {IToastr} from './toastr.model'; 

export let TOASTR_TOKEN = new InjectionToken<IToastr>('toastr'); 

然后app.module.ts我包括它作为提供者:

import {TOASTR_TOKEN} from './common/tokens'; 

let toastr = window['toastr']; 

... 
providers: [ 
    ... 
    {provide: TOASTR_TOKEN, useValue: toastr} 
], 
... 
+0

:/ /rahulrsingh09.github.io/AngularConcepts/faq)关于如何在Angular中包含第三方库 –

回答

2

好吧,经过一番研究,我发现了一个解决方案,我的问题米什么化险为夷是出口,我用它来分配给供应商的useValue属性变量:

import {TOASTR_TOKEN} from './common/tokens'; 

export let toastr = window['toastr']; 

... 
providers: [ 
    ... 
    {provide: TOASTR_TOKEN, useValue: toastr} 
], 

我不认为它会像这样检查此[链接](HTTPS ...