私のアプリケーションにng2-toastrを実装しようとしています。私は"@angular/compilercli": "^4.3.6",
です。私は次のインターセプターをHTTPのフォームエラーを傍受する必要があります。ng2-toastrがAngular 4.3.6サービスで呼び出されたときに適切なCSSを適用しない
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()
をimporintingした後、私のRootViewContainerRefを設定しています。それがうまくコンパイルし、私もコンソールでのサービスのconsole.log('---> status:', evt.status);
ラインからログを参照してくださいが、どのtoastrが表示され表示されていないいくつかの理由
constructor(public toastr: ToastsManager, vRef: ViewContainerRef) {
this.toastr.setRootViewContainerRef(vRef);
}
。しかし、私はブラウザでコンポーネントを検査するときにトーストコンテナを見ることができます。私はここで何が欠けているのか分からない。どんなガイドも感謝します。実装で何が問題になっていますか?
サービスがNature.https://stackoverflow.com/questions/40636840/how-can-i-injectでシングルトンであるため、サービスに「ViewContainerRef」を追加することはできません-viewcontainer -ref-into-a-service –