哪怕是新vue项目,使用 vite 的 import.meta.glob 都会报严格模式错误
我有一个vue2项目,我想将他转移到vue3上,但在动态路由上出现了严格模式的问题,最初我的代码是复制的,不清楚到底是哪里的问题,于是我重新建了一个项目打算重写动态路由部分,但哪怕是新项目都无法动态import.
系统为macos15
我从webstorm上新建项目, node为20.18.1(ws里下载的), create-vite为6.1.1.
package.json:
{
"name": "test",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.5.13",
"vue-router": "^4.5.0"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.2.1",
"vite": "^6.0.5"
}
}
test.js 在main.js中导入
//test.js
const mo = import.meta.glob("./components/**/*")
console.log(mo)
export default mo
文件列表
结果:
[异常:TypeError: 'caller', 'callee', and 'arguments' ....
"./components//.vue"和"./components//"我都试过了,均会报错
所以说到底是哪里的问题....
这是正常现象
import.meta.glob
方法返回一个对象,类型为Record<string, () => Promise<unknown>>
这个对象的键是文件路径,值是一个
import
函数调用这个函数相当于导入路径对应的模块
因为严格模式下函数的
arguments
和caller
被禁用,所以会报错你应该调用该函数得到一个 Promise,该 Promise fufilled 之后将返回路径对应的模块