如何 avoid/fix "Auth0Lock is not defined" 异常

How to avoid/fix "Auth0Lock is not defined" exception

我正在尝试使用 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');
  };
}

我注入了服务并配置了提供者。一切都正确连接,但即使已定义,它也找不到 Auth0Lock。 每次到达 lock = new Auth0Lock('ID', 'DOMAIN', {}); 时它就会爆炸。

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

我需要添加到 index.html:

<script src="https://cdn.auth0.com/js/lock/10.8/lock.min.js"></script>

通过https://github.com/auth0/lock/issues/588

接受的答案很好。我确实收到 无法找到名称 'require' 错误。 我没有使用 'declare const require',而是这样导入的:

import Auth0Lock from 'auth0-lock';