如何排除某些服务,如登录、从拦截器 Angular 5 注册、HttpClient [英] How to exclude some services like login, register from interceptor Angular 5, HttpClient

查看:30
本文介绍了如何排除某些服务,如登录、从拦截器 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆