【发布时间】:2017-10-30 11:41:45
【问题描述】:
我正在尝试在我的应用程序中实现ng2-toastr。我在"@angular/compilercli": "^4.3.6",。我有以下拦截器来拦截来自 Http 的错误。
export class InterceptorService implements HttpInterceptor {
constructor(public toastr:ToastsManager) {
}
intercept(req:HttpRequest<any>,
next:HttpHandler):Observable<HttpEvent<any>> {
//Inspection removed for this file for rxjs.
//noinspection TypeScriptValidateTypes
return next.handle(req).do(evt => {
if (evt instanceof HttpResponse) {
this.toastr.success('You are awesome!', 'Success!');
console.log('---> status:', evt.status);
// console.log('---> filter:', req.params.get('filter'));
}
}, err => {
if (err instanceof HttpErrorResponse) {
this.toastr.error('This is not good!', 'Oops!');
//toastr here
}
console.log(err);
});
}
}
在我的组件中,在我的 @NgModule 声明中导入 ToastModule.forRoot() 之后,我正在设置我的 RootViewContainerRef。
constructor( public toastr: ToastsManager, vRef: ViewContainerRef) {
this.toastr.setRootViewContainerRef(vRef);
}
由于某种原因,它编译得很好,我还在控制台中看到了来自console.log('---> status:', evt.status); 服务行的日志,但没有看到任何 toastr 显示。但是,当我在浏览器中检查组件时,我可以看到 toast 容器。我不知道我在这里错过了什么。任何指南将不胜感激。我的实施有什么问题?
【问题讨论】:
标签: javascript angular typescript npm angular-services