Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular HTTP拦截器:您在需要流的位置提供了“未定义”_Angular_Angular6_Angular Http Interceptors - Fatal编程技术网

Angular HTTP拦截器:您在需要流的位置提供了“未定义”

Angular HTTP拦截器:您在需要流的位置提供了“未定义”,angular,angular6,angular-http-interceptors,Angular,Angular6,Angular Http Interceptors,我正在使用angular 6,试图实现http拦截器,它给我提供了“未定义”,在尝试登录时需要流的地方。我没有jwt令牌,我有一个x会话 拦截器 登录服务 登录组件 如何修复错误?我想如果我使用jwt,就不会有错误,因为大多数文章都推荐上面所示的方法或类似的方法。我不知道我的解决方案是否是最好的,但它似乎是有效的,如果有人有更好的解决方案,我会接受他的 拦截器 登录服务 请注意,我添加了一条else语句,如果用户登录,请添加令牌,否则,不要添加它。是否设置了断点以确定哪一条未定义?@NgocNa

我正在使用angular 6,试图实现http拦截器,它给我提供了“未定义”,在尝试登录时需要流的地方。我没有jwt令牌,我有一个x会话

拦截器

登录服务

登录组件


如何修复错误?我想如果我使用jwt,就不会有错误,因为大多数文章都推荐上面所示的方法或类似的方法。

我不知道我的解决方案是否是最好的,但它似乎是有效的,如果有人有更好的解决方案,我会接受他的

拦截器

登录服务


请注意,我添加了一条else语句,如果用户登录,请添加令牌,否则,不要添加它。

是否设置了断点以确定哪一条未定义?@NgocNamNguyen明天上午4点就可以了,很抱歉忘了这么做错误消息是否指定了它是哪一行?
  constructor(private injector: Injector) { }
  intercept(req, next) {
    const auth = this.injector.get(AuthService);
    const token = auth.getToken();
    if (token) {
      const tokenizedReq = req.clone({
        setHeaders: ({
          'Content-Type': 'application/json',
          'x-session': token
        })

      });
      return next.handle(tokenizedReq);
    }
  login(username, password) {
    const data = {
      username: username,
      password: password
    };
    const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
    return this.http.post(this.login_url, data, { headers: headers, observe: 'response' });
  }
  getToken() {
    return JSON.parse(localStorage.getItem('token'));
  }
  onLogin() {
    this.auth.login(this.username, this.password).subscribe(data => {
      if (localStorage.getItem('data') === null) {
        localStorage.setItem('data', JSON.stringify(data));
      }
      const token = data.headers.get('x-session');
      const expiry = data.headers.get('x-session-expiry');
      localStorage.setItem('token', JSON.stringify(token));
      localStorage.setItem('token-expiry', JSON.stringify(expiry));
      this.router.navigate(['']);
    }, err => {
      console.log(err);
    });
  }
constructor(private injector: Injector) { }
  intercept(req, next) {
    const auth = this.injector.get(AuthService);
    const token = auth.getToken();
    if (token) {
      const tokenizedReq = req.clone({
        setHeaders: ({
          'Content-Type': 'application/json',
          'x-session': token
        })

      });
      return next.handle(tokenizedReq);
    } else {
      const tokenizedReq = req.clone({
        setHeaders: {
          'Content-Type': 'application/json'
        }
      });
      return next.handle(tokenizedReq);
    }
  }
 login(username, password) {
    const data = {
      username: username,
      password: password
    };
    return this.http.post(this.login_url, data, { observe: 'response' });
  }