1

我正在使用 Angular 7 并从此链接安装 ng6-toastr-notifications 应用程序

我尽可能多地添加了此代码。这是我删减的 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]

})

这是 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');
  }

使用 Visual Studio Code 时没有出现编译错误。我在 Google Chrome 浏览器控制台窗口中收到错误,如下所示:

ERROR Error: StaticInjectorError(AppModule)[InjectionToken 
HTTP_INTERCEPTORS -> ToasterComponent]: 
  StaticInjectorError(Platform: core)[InjectionToken HTTP_INTERCEPTORS -> 
ToasterComponent]: 
    NullInjectorError: No provider for ToasterComponent! 

这是 error.interceptor.ts import { Injectable } from '@angular/core';

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);
    }))
}
}
4

1 回答 1

1

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');
  }
于 2019-01-13T16:14:25.010 回答