angular 8: "Schematic "拦截器在 Angular cli 中生成拦截器时出现“未在集合中找到”错误
angular 8: "Schematic "interceptor" not found in collection" error while generating interceptor in Angular cli
我正在执行命令
ng g interceptor error
其中错误是拦截器的名称
但遇到如下问题:
An unhandled exception occurred: Schematic "interceptor" not found in collection "@schematics/angular".
我们必须先安装任何软件包吗?
似乎在 CLI 9.0.0
中添加了对生成拦截器的原理图支持
因此,您可以升级到 Angular 9,或者编写您自己的自定义原理图,但在短期内,如果您不能/不想升级到 Angular 9,自己编写拦截器可能比生成拦截器更容易。
如果有人想要创建加载拦截器。下面是一个示例加载拦截器,如下所示创建它然后注入
LoadingInterceptor, LoadingIndicatorInterceptor in the provider's array of the parent module.
import { LoadingIndicatorService } from './file-path';
import { Injectable } from '@angular/core';
import {
HttpEvent,
HTTP_INTERCEPTORS,
HttpInterceptor,
HttpHandler,
HttpRequest,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { finalize, tap } from 'rxjs/operators';
@Injectable()
export class LoadingIndicatorInterceptor implements HttpInterceptor {
constructor(public loadingIndicatorService: LoadingIndicatorService) {}
intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// emit onStarted event before request execution
this.loadingIndicatorService.onStarted(req);
return next
.handle(req)
// emit onFinished event after request execution
.pipe(
finalize(() => this.loadingIndicatorService.onFinished(req)), tap( r => {
})
);
}
}
export const LoadingInterceptor = {
provide: HTTP_INTERCEPTORS,
useClass: LoadingIndicatorInterceptor,
multi: true,
};
providers: [ LoadingInterceptor, LoadingIndicatorInterceptor]
// 这一行应该在您希望拦截器可用的模块中
我正在执行命令
ng g interceptor error
其中错误是拦截器的名称
但遇到如下问题:
An unhandled exception occurred: Schematic "interceptor" not found in collection "@schematics/angular".
我们必须先安装任何软件包吗?
似乎在 CLI 9.0.0
中添加了对生成拦截器的原理图支持因此,您可以升级到 Angular 9,或者编写您自己的自定义原理图,但在短期内,如果您不能/不想升级到 Angular 9,自己编写拦截器可能比生成拦截器更容易。
如果有人想要创建加载拦截器。下面是一个示例加载拦截器,如下所示创建它然后注入 LoadingInterceptor, LoadingIndicatorInterceptor in the provider's array of the parent module.
import { LoadingIndicatorService } from './file-path';
import { Injectable } from '@angular/core';
import {
HttpEvent,
HTTP_INTERCEPTORS,
HttpInterceptor,
HttpHandler,
HttpRequest,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { finalize, tap } from 'rxjs/operators';
@Injectable()
export class LoadingIndicatorInterceptor implements HttpInterceptor {
constructor(public loadingIndicatorService: LoadingIndicatorService) {}
intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// emit onStarted event before request execution
this.loadingIndicatorService.onStarted(req);
return next
.handle(req)
// emit onFinished event after request execution
.pipe(
finalize(() => this.loadingIndicatorService.onFinished(req)), tap( r => {
})
);
}
}
export const LoadingInterceptor = {
provide: HTTP_INTERCEPTORS,
useClass: LoadingIndicatorInterceptor,
multi: true,
};
providers: [ LoadingInterceptor, LoadingIndicatorInterceptor]
// 这一行应该在您希望拦截器可用的模块中