我们可以在Angular中拦截js请求吗?



我已经在angular中编写了http拦截器,但我无法在拦截器处理程序中看到js请求。我想拦截一些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.
}
}

让我知道是否有其他东西拦截js请求?

注意:我正在研究角延迟加载,想要拦截块加载请求并更改请求的主机。

拦截器需要在app模块中注册为提供商。

@NgModule({
imports:      [ BrowserModule, FormsModule, HttpClientModule ],
providers:    [ 
{ provide: HTTP_INTERCEPTORS, useClass: MyHttpInterceptor, multi: true }
],
declarations: [ AppComponent ],
bootstrap:    [ AppComponent ]
})
export class AppModule { }

和@Matthieu写的,只要你将使用httpclient触发调用将工作。

拦截所有请求的唯一方法是在ServiceWorker中使用fetch监听器。

self.addEventListener('fetch', function(event) {
const url = event.request.url;
// do what you need 
})

最新更新