Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

怎么让vue-cli支持顶层await

如题 背景是需要在一个入口文件动态引进其他插件,然后再将这些插件暴露出去,顶层await这个语法目前在chrome可以正常使用,但是在vue-cli中会报编译错误,不知有啥解决方案
https://github.com/tc39/propo...
image.png

const libPath = [
    'client/client',
    'client/lib',
    'page_api/api',
    'report/beaconReport',
    'util/util',
]
const libPathHandlers = libPath.map( path => import(`@lib/${path}`) );
const modules = await Promise.all(libPathHandlers)
// todo....  export modules
// compile error

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
const libPath = [
  'client/client',
  'client/lib',
  'page_api/api',
  'report/beaconReport',
  'util/util',
];

const modules = {};

for (const path of libPath) {
  const module = require(`@lib/${path}`);
  modules[path] = module;
}

export default modules;

这种需求可以用require来实现
要挂在哪个属性上自己考虑,例子里面直接直接挂在path
如果是用export default导出来的对象,要取module.default


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...