我正在尝试运行用于在我的React项目中创建身份验证服务的教程代码。
这是他们希望我运行的代码示例:
// src/Auth/Auth.js
import auth0 from 'auth0-js';
export default class Auth {
auth0 = new auth0.WebAuth({
domain:'domain.auth0.com',
clientID: 'clientId',
redirectUri: 'http://localhost:3000/callback',
audience: 'https://myproject.auth0.com/userinfo',
responseType: 'token id_token',
scope: 'openid'
});
login() {
this.auth0.authorize();
}
}
当我运行它时,它会引发有关“导入”和“导出”关键字的错误。
所以我将其更改为:
const auth0 = require("auth0-js");
class Auth {
auth = new auth0.WebAuth({
domain: 'mydomain.auth0.com',
clientID: 'clientID',
redirectUri: 'http://localhost:3000/callback',
audience: 'https://myproject.auth0.com/userinfo',
responseType: 'token id_token',
scope: 'openid'
});
login() {
this.auth.authorize();
}
}
module.exports = Auth;
但这给了我这个错误:
/Users/myname/my project/app/services/auth.js:4
auth = new auth0.WebAuth({
^
SyntaxError: Unexpected token =
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
正确的语法是什么?
最佳答案
使用--harmony
标志,instance class fields仅在节点> = 10中的supported。
您可以改用getter,该支持不带任何标志。
class Auth {
get auth() {
if(!this._auth) {
this._auth = new auth0.WebAuth({ /* ... */ });
}
return this._auth;
}
login() {
this.auth.authorize();
}
}
或者只是将其设置在
constructor
class Auth {
constructor() {
this.auth = new Auth0.WebAuth({ /* ... */ });
}
}
或者,您可以使用babel来转换代码。
关于javascript - 运行示例代码时出错-导出类中的类-React Node.js中的Auth0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50828380/