Angularjs 在http侦听器服务中使用$window

Angularjs 在http侦听器服务中使用$window,angularjs,ecmascript-6,Angularjs,Ecmascript 6,我试图在http拦截器中使用$window对象,以便可以引用localStorage 问题是,每当我删除以下行时,我的web应用程序都会工作。但当我添加它时,没有页面加载,只有一个白色屏幕,但也没有错误 let {$window} = this; 我的主模块配置: $httpProvider.interceptors.push('authInterceptor'); MyauthInterceptor服务类别: export class authInterceptor { constru

我试图在http拦截器中使用$window对象,以便可以引用localStorage

问题是,每当我删除以下行时,我的web应用程序都会工作。但当我添加它时,没有页面加载,只有一个白色屏幕,但也没有错误

let {$window} = this;
我的主模块配置:

$httpProvider.interceptors.push('authInterceptor');
MyauthInterceptor服务类别:

export class authInterceptor {
  constructor($window) {
    this.$window = $window;
  }

  request(config) {
    let {$window} = this;

    return config;
  }

  responseError(response) {
    return response;
  }

}
authInterceptor.$inject = ['$window'];

考虑到浏览器支持ES6(而且它是受支持的,因为拦截器服务是一个可能正在传输的类),当
let{$window}=this
时唯一可能导致问题的是
this
不是类实例,甚至不是对象

拦截器函数是用作回调和调用的函数。这意味着
将在其内部以严格模式
未定义

与用作回调的任何其他类方法一样,这些类方法应该绑定到适当的上下文,以便保留它并能够使用注入的服务:

  constructor($window) {
    this.$window = $window;
    this.request = this.request.bind(this);
    this.responseError = this.responseError.bind(this);
  }

那就别用了?就用这个吧。$window?或者只使用let w=这个。$window?我猜你的transpiler不支持这种语法。这种语法也不起作用,而且这种语法在我的其他模块中也起作用。在构造函数中记录$window对象会得到一个有效的window对象。。hmmI发现问题在于“this”在请求函数中没有定义。实现其他完全相同的服务可以得到正确的“this”对象。所以我猜测它与Http拦截器有关。你猜怎么解决这个问题@谢谢!这正是我要找的。