无法访问角度2中的localStorage

时间:2017-04-19 13:16:58

标签: angular local-storage

我有一个使用angular cli生成的项目。 我使用auth0进行身份验证,但是我收到一条错误消息,指出无法找到本地存储 auth.service.ts

import { Injectable }      from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
import { Router, NavigationStart } from '@angular/router';
import 'rxjs/add/operator/filter';

// Avoid name not found warnings
let Auth0Lock: any = require('auth0-lock').default;

@Injectable()
export class AuthService {
  // Configure Auth0
  lock = new Auth0Lock('vo2lImwjnYPMKIpnRmV95Cmqt8nBfkfk', 'zied.eu.auth0.com', {});

  constructor(public router: Router) {
  
    // Add callback for lock `authenticated` event
    this.lock.on("authenticated", (authResult) => {
      localStorage.setItem('id_token', authResult.idToken);
    });
  }

  public login() {
    // Call the show method to display the widget.
    this.lock.show();
  }

  public authenticated() {
    // Check if there's an unexpired JWT
    // This searches for an item in localStorage with key == 'id_token'
    return tokenNotExpired('id_token');
  }

  public logout() {
    // Remove token from localStorage
    localStorage.removeItem('id_token');
  }
}

强文 localStorage是默认关键字 我应该导入localStorage吗?但从哪里来? 使用窗口

时,不仅localStorage我也会遇到同样的错误

0 个答案:

没有答案