[英]Cannot find module in VueJS Error using a JSON file
I was given a task in finding a way to dynamically import pages inside vuejs projects;我的任务是找到一种在 vuejs 项目中动态导入页面的方法; built with a CLI tool the team I am in, is working on.
使用我所在团队正在开发的 CLI 工具构建。 I tried using functions as a string ("() => import(...)") and then eval that string and that did not work.
我尝试将函数用作字符串 ("() => import(...)") 然后评估该字符串,但没有奏效。 Currenly I have used:
目前我使用过:
{
"routes": [
{
"name": "Login",
"path": "/login",
"component": "../../src/pages/auth/login/*"
},
{
"name": "Register",
"path": "/register",
"component": "@/pages/auth/register"
}
]
}
then I use a "driver" to then pass into our routes.ts file:然后我使用“驱动程序”传递到我们的 routes.ts 文件中:
import * as dynamicRoutes from './routes.json';
const routes: any[] = [];
dynamicRoutes.default.routes.forEach((elem: any) => {
const component = async () => await require(elem.component).then((comp: any) => comp);
routes.push({
name: elem.name,
path: elem.path,
component,
});
});
export default routes;
Which im getting "cannnot find module" errors.我收到“找不到模块”错误。 Also when i log the output i get this when i check the component function: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.invokeGetter.
此外,当我记录输出时,我在检查组件函数时得到这个:TypeError: 'caller'、'callee' 和 'arguments' 属性可能无法在严格模式函数上访问,或者在函数中调用它们的参数对象。调用Getter。
Is there a way that i can dynamically pass routes using this json file?有没有办法可以使用这个 json 文件动态传递路由? (Because we are reading and writing to/from the json file to keep/update the routes)
(因为我们正在读取和写入 json 文件以保持/更新路由)
你可以使用动态导入来延迟加载 Vue 组件,它甚至在你安装 vue-router 时默认包含在内
const component = () => import(elem.component)
when you say dynamically import pages, I believe it is something related to lazy load the component dynamically ?当您说动态导入页面时,我认为这与动态延迟加载组件有关? If so, you need to have a HOC for all your route components which can basically use es6 import() (promise based) to fetch the component .
如果是这样,您需要为所有路由组件设置一个 HOC,它基本上可以使用 es6 import()(基于承诺)来获取组件。 You can also use webpack magic comments for the same(if you are using webpack).
您也可以使用 webpack 魔术注释(如果您使用的是 webpack)。 hope it helps
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.