Javascript 在页面加载时未调用Angular 8基本拦截器
我正在为我的angular 8项目添加一个拦截器。添加承载令牌。不幸的是,拦截器似乎没有被调用。我的代码: 我已经:Javascript 在页面加载时未调用Angular 8基本拦截器,javascript,angular,typescript,interceptor,angular8,Javascript,Angular,Typescript,Interceptor,Angular8,我正在为我的angular 8项目添加一个拦截器。添加承载令牌。不幸的是,拦截器似乎没有被调用。我的代码: 我已经: 在app.module.ts文件中添加了HttpClientModule、HTTP_拦截器和HttpConfigInterceptor导入 在BrowserModule之后的imports部分中添加了HttpClientModule 在providers部分添加了{provide:HTTP_拦截器,useClass:HttpConfigInterceptor,multi:true
HttpClientModule
、HTTP_拦截器
和HttpConfigInterceptor
导入BrowserModule
之后的imports
部分中添加了HttpClientModule
{provide:HTTP_拦截器,useClass:HttpConfigInterceptor,multi:true}
从'@angular/core'导入{NgModule};
从“@angular/platform browser”导入{BrowserModule,Title}”;
从“@angular/common/HTTP”导入{HttpClientModule,HTTP_INTERCEPTORS};
从“./httpconfig.interceptor”导入{HttpConfigInterceptor};
从“./app routing.module”导入{AppRoutingModule};
从“./app.component”导入{AppComponent};
从“./navbar/navbar.component”导入{NavbarComponent};
从“./header/header.component”导入{HeaderComponent};
从“./footer/footer.component”导入{FooterComponent};
从“./home/home.component”导入{HomeComponent};
从“./privacy policy/privacy policy.component”导入{PrivacyPolicyComponent};
从“./terms of use/terms of use.component”导入{TermsOfUseComponent};
从“./snippets/snippets.component”导入{SnippetsComponent};
从“./snippet/snippet.component”导入{SnippetComponent};
从“./not found/not found.component”导入{NotFoundComponent};
@NGD模块({
声明:[
应用组件,
导航组件,
头部组件,
页脚组件,
HomeComponent,
私隐部分,
术语使用组件,
代码段组件,
代码段组件,
NotFoundComponent
],
进口:[
浏览器模块,
批准模块,
HttpClientModule
],
供应商:[
标题
{提供:HTTP_拦截器,useClass:HttpConfigInterceptor,multi:true}
],
引导:[AppComponent]
})
导出类AppModule{}
我的拦截器文件:
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpInterceptor,HttpRequest,HttpHandler,HttpEvent};
从“rxjs”导入{Observable};
@可注射()
导出类HttpConfigInterceptor实现HttpInterceptor{
构造函数(){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
控制台日志(请求);
下一步返回。处理(请求);
}
}
我只想让console.log工作
Thks^^.应将HttpConfigInterceptor添加到Providers您有什么要拦截的吗?当您使用HttpClient时,HttpInterceptor会触发。@Mattworterspieler,谢谢您的回答,我只想第一次显示我的console.log,之后,它允许自动添加承载令牌。但是,我不明白为什么我的console.log不能工作@塔诺,谢谢你的回答,它已经添加到提供者部分。@François只有当你有东西要拦截http请求时,拦截方法才会触发。ie:服务中的this.httpClient.get('url')将在调用时触发拦截方法。希望我清楚。HttpConfigInterceptor应该添加到Providers您有什么要拦截的吗?当您使用HttpClient时,HttpInterceptor会触发。@Mattworterspieler,谢谢您的回答,我只想第一次显示我的console.log,之后,它允许自动添加承载令牌。但是,我不明白为什么我的console.log不能工作@塔诺,谢谢你的回答,它已经添加到提供者部分。@François只有当你有东西要拦截http请求时,拦截方法才会触发。ie:服务中的this.httpClient.get('url')将在调用时触发拦截方法。希望我明白了。