如何避免/修复"Auth0Lock未定义"异常

Siy*_*iya 6 http-token-authentication auth0 angular2-jwt

我正在尝试使用Auth0进行社交登录,但我不断获得未定义引用的例外.

这是身份验证服务

import { Injectable }      from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';

// Avoid name not found warnings
declare var Auth0Lock: any;

@Injectable()
export class AuthService {
// Configure Auth0

lock = new Auth0Lock('I have set the ID correctly here', 'and the domain as well', {});

constructor() {
// 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();
};

public logout() {
   // Remove token from localStorage
    localStorage.removeItem('id_token');
  };
}
Run Code Online (Sandbox Code Playgroud)

我注入了服务和配置的提供程序.一切都正确连接,但Auth0Lock即使定义也无法找到.每次到达lock = new Auth0Lock('ID', 'DOMAIN', {});它时都会爆炸.

Siy*_*iya 6

我替换declare var Auth0Lock: any;const Auth0Lock = require('auth0-lock').default;并修复了问题.