本文介绍了如何从拦截器Angular 5,HttpClient中排除某些服务(例如登录,注册)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用拦截器排除某些服务.

i wanted to exclude some services using interceptor.

app.module.js

providers: [
    UserService,
    RolesService,
    {
        provide: HTTP_INTERCEPTORS,
        useClass: TokenInterceptor,
        multi: true
      },
],

Login.service.ts

return this.httpClient.post(this.appUrl + '/oauth/token', body.toString(), { headers, observe: 'response' })
.map((res: Response) => {
  const response = res.body;
  this.storeToken(response);
  return response;
})
.catch((error: any) => {
  ErrorLogService.logError(error);
  return Observable.throw(new Error(error.status));
  });
}

推荐答案

不仅要根据要求在注释中提供答案,还必须提供;-)

To give this an answer not only in comments as requested ;-)

要从拦截器中排除某些服务(甚至是用于不同组件的相同服务),最好将您的应用程序拆分为模块,并仅在需要时才在模块中提供拦截器.例如,登录后或在管理区域内.

To exclude some services (or even the same services used in different components) from an interceptor it's best to split your application into modules and provide the interceptor only in modules where it's needed. For example after logging in or inside of an admin area.

甚至可以使用@Component声明的providers属性为单个组件提供拦截器.

The interceptor may be even provided for single components using the @Component declaration's providers property.

这篇关于如何从拦截器Angular 5,HttpClient中排除某些服务(例如登录,注册)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:49