试图从angular2-jwt使用tokenNotExpired,但在模块中得到错误

时间:2018-06-10 03:43:55

标签: javascript angular angular2-jwt

我正在尝试使用angular2-jwt的tokenNotExpired来检查用户是否已登录。但是当我实现这个时,我在cli中得到以下错误: -

ERROR in node_modules/angular2-jwt/angular2-jwt.d.ts(3,10): error TS2305: Module '"d:/Visual Studio/asp.net/mean_blog/client/node_modules/rxjs/Observable"' has no exported member 'Observable'.
node_modules/rxjs/Observable.d.ts(1,15): error TS2307: Cannot find module 'rxjs-compat/Observable'.

我使用“npm install angular2-jwt @ latest --save”命令安装angular2-jwt。

以下是auth.service.ts代码: -

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { map, take } from 'rxjs/operators';
import { tokenNotExpired } from 'angular2-jwt';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  authToken;
  user;
  options;
  constructor(private http: Http) { }

  createAuthenticationHeaders(){
    this.loadToken();
    this.options = new RequestOptions({
      headers:new Headers({
        'Content-Type':'application/json',
        'authorization':this.authToken
      })
    })
  }

  loadToken(){
    this.authToken = localStorage.getItem('token');
  }

  registerUser(user) {
    return this.http.post('/api/auth/register', user).pipe(map(res => res.json()));
  }
  checkUsername(username) {

    return this.http.get('/api/auth/checkUsername/' + username).pipe(map(res => res.json()));
  }
  checkEMail(email) {
    return this.http.get('/api/auth/checkEmail/' + email).pipe(map(res => res.json()));
  }

  login(user){
    return this.http.post('/api/auth/login',user).pipe(map(res=>res.json()));
  }
  logout(){
    this.authToken = null;
    this.user = null;
    localStorage.clear();
  }
  storeUserData(token, user){
    localStorage.setItem('token',token);
    localStorage.setItem('user',JSON.stringify(user));
    this.authToken = token;
    this.user = user
  }

  getProfile(){
    this.createAuthenticationHeaders();
    return this.http.get('/api/auth/profile',this.options).pipe(map(res=>res.json()));
  }
  loggedIn() {
    return tokenNotExpired();
  }
}

我还检查了package.json中的依赖项,该依赖项也正确添加。 我不知道这个错误发生在哪里。

1 个答案:

答案 0 :(得分:0)

rxjs的某些功能在angular 6中已弃用,因此我使用rxjs-compat来实现向后兼容性。它解决了这个问题。