我正在尝试在需要时动态导入类。
以下代码有效;
...
methods: {
handleLogin(e) {
e.preventDefault()
this.$store.dispatch('login', this.loginForm)
.then((response) => {
console.log('login page response: ', response)
if (response.id_user !== undefined) {
this.$router.push({ path: '/' })
}
})
.catch((e) => {
console.log('ты внутри ошибки: ', e);
});
},
websocketAuth(e) {
e.preventDefault()
console.log('login: ', this.loginForm.username, this.loginForm.password)
this.$store.dispatch("logoutEverywhere", {
user_login: this.loginForm.username,
user_password: this.loginForm.password
})
.then((resp) => {
let data = { userId: resp, page: 'login' }
socketUrl.emit('logoutEverywhere', data, (flag) => {
if(flag) {
this.$store.dispatch('duplicateLoginClear')
}
console.log(flag)
})
console.log('1 ', resp)
})
}
}
...
在函数中;
import TestCommand from "./commands/TestCommand.js"; //At top of the file
还有TestCommand.js;
let test = new TestCommand();
console.log(test.getName());
但是,如果我试图在函数中完成相同的事情,就像这样;
import Command from "./Command.js";
export default class TestCommand extends Command {
constructor() {
super();
this.name = "Test";
}
getName() {
return this.name;
}
execute() {
}
}
我得到一个错误; (节点:14264)UnhandledPromiseRejectionWarning:TypeError:TestCommand不是构造函数
关于如何实现此目标的任何建议?