[英]Using ng6-toastr-notification get error “No provider for ToasterComponent”
I am using angular 7 and installing an ng6-toastr-notifications app from this link 我正在使用angular 7并从此链接安装ng6-toastr-notifications应用程序
I added as much of this code as allowed. 我添加了尽可能多的这段代码。 This is my abridged app.module.ts file: 这是我的简化的app.module.ts文件:
import {BrowserAnimationsModule} from '@angular/platform-browser/animations'
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'
import { ToastrModule } from 'ng6-toastr-notifications'
import { JwtInterceptor, ErrorInterceptor } from './_helpers';
import { LogoffComponent } from './logoff/logoff.component';
import { ToasterComponent } from './_helpers/toaster/toaster.component'
@NgModule({
declarations: [
AppComponent,
StocksComponent,
LoginComponent,
LoginHomeComponent,
HomeComponent,
NavComponent,
RegisterComponent,
LogoffComponent,
ToasterComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
MatButtonModule,
MatInputModule,
MatCardModule,
MatToolbarModule,
FormsModule,
ReactiveFormsModule,
MatListModule,
AppRoutingModule,
ToastrModule.forRoot()
],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }
],
bootstrap: [AppComponent]
})
This is the ToasterComponent.ts code: 这是ToasterComponent.ts代码:
import { Component} from '@angular/core';
import { ToastrManager } from 'ng6-toastr-notifications'
@Component({
selector: 'app-toaster',
templateUrl: './toaster.component.html',
styleUrls: ['./toaster.component.css']
})
export class ToasterComponent {
constructor(public toastr: ToastrManager) { }
showSuccess() {
this.toastr.successToastr('This is success toast.', 'Success!');
}
show401Error() {
this.toastr.errorToastr('invalid authentication credentials');
}
I do not get a compile error using Visual Studio Code. 我没有使用Visual Studio Code遇到编译错误。 I get the error in the Google Chrome browser console window as shown below: 我在Google Chrome浏览器控制台窗口中收到错误,如下所示:
ERROR Error: StaticInjectorError(AppModule)[InjectionToken
HTTP_INTERCEPTORS -> ToasterComponent]:
StaticInjectorError(Platform: core)[InjectionToken HTTP_INTERCEPTORS ->
ToasterComponent]:
NullInjectorError: No provider for ToasterComponent!
Here is the error.interceptor.ts import { Injectable } from '@angular/core'; 这是来自'@ angular / core'的error.interceptor.ts import {Injectable};
import { ToasterComponent } from './toaster/toaster.component'
import { AuthenticationService } from '../_services';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService,
public toaster: ToasterComponent) {}
intercept(request: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status === 401 ) {
// send message to the client
// auto logout if 401 response returned from api
this.authenticationService.logout();
location.reload(true);
}
const error = err.error.message || err.statusText;
return throwError(error);
}))
}
}
Adding injectable to my code got rid of the error: 将可注射物添加到我的代码中摆脱了错误:
import { Injectable } from '@angular/core';
import { Component} from '@angular/core';
import { ToastrManager } from 'ng6-toastr-notifications'
@Component({
selector: 'app-toaster',
templateUrl: './toaster.component.html',
styleUrls: ['./toaster.component.css']
})
@Injectable({ providedIn: 'root' })
export class ToasterComponent {
constructor(public toastr: ToastrManager) { }
showSuccess() {
this.toastr.successToastr('This is success toast.',
'Success!');
}
show401Error() {
this.toastr.errorToastr('invalid authentication
credentials');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.