一、错误原因
最高层中不让使用 await
二、解决方案
1.引入vite-plugin-top-level-await
npm install vite-plugin-top-level-await -D
2.在vite.config.js配置此插件
import topLevelAwait from 'vite-plugin-top-level-await'
export default defineConfig({
plugins: [
topLevelAwait({
// The export name of top-level await promise for each chunk module
promiseExportName: '__tla',
// The function to generate import names of top-level await promise in each chunk module
promiseImportName: i => `__tla_${i}`
})
]
});
3.重新打包