这个问题涉及到Chrome浏览器。
我正在尝试动态导入一个定义为类的JavaScript模块。
我想知道如何获取目标代码中已导入类的名称。 以下是我的目标代码,但它无法工作。
我正在尝试动态导入一个定义为类的JavaScript模块。
// data-table.js
export class DataTable extends HTMLElement{
constructor() {
super();
}
static get tagName() {
return 'data-table';
}
}
我想知道如何获取目标代码中已导入类的名称。 以下是我的目标代码,但它无法工作。
// router.js
...//some code
if(route === matched){
// dynamically import the component
import(route.component)
.then( module => {
// anyway to get classname DataTable here??
})
};
...//rest of the code
以下是一个显而易见的实现,它能够工作(因为我硬编码了模块类名)
// router.js
...//some code
if(route === matched){
// dynamically import the component
import("components/data-table.js")
.then( {DataTable} => {
cost t = DataTable.tagName;
// code to handle module stuff
})
};
...//rest of the code
这里有一个类似的问题(链接),但没有给出可行的答案,不过那个问题是关于Webpack的,而我是直接在浏览器中尝试。 为什么我要获取类名? 因为这使得我的代码更简单。