[英]Can we intercept js request in Angular?
I have written http interceptor in angular but I cannot see js request coming in the interceptor handler.我已经在 angular 中编写了 http 拦截器,但我看不到拦截器处理程序中的 js 请求。 I wanted to intercept the some js request and do some handling around it.我想拦截一些 js 请求并对其进行一些处理。 I am looking to intercept request like Request URL: http://localhost:5000/chunk1.js
我正在寻找拦截像Request URL: http://localhost:5000/chunk1.js
这样的请求
@Injectable()
export class SimpleInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req); // js request is not coming here.
}
}
Let me know if there is something else to intercept js request?让我知道是否还有其他拦截js请求的方法?
Note: I am working on angular lazy-loading and want to intercept the chunk loading request and change the host of request.注意:我正在处理 angular 延迟加载,想拦截块加载请求并更改请求的主机。
Interceptors need to be registered as providers in app module.拦截器需要在应用程序模块中注册为提供者。
@NgModule({ imports: [ BrowserModule, FormsModule, HttpClientModule ], providers: [ { provide: HTTP_INTERCEPTORS, useClass: MyHttpInterceptor, multi: true } ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
And as @Matthieu wrote, just if you will use httpclient to triger calls will work.正如@Matthieu 所写,只要你使用 httpclient 来触发调用就可以了。
The only way to intercept all requests is in the ServiceWorker with the fetch listener.拦截所有请求的唯一方法是在带有获取侦听器的 ServiceWorker 中。
self.addEventListener('fetch', function(event) {
const url = event.request.url;
// do what you need
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.