vue3动态(路由)异步导入()=>import(path)异常的问题解决.

655 阅读1分钟
//以动态导入路由为例
const path = '@/views/StandardList.vue';
router.addRoute({
  path: '/example/blank/blank',
  name: 'StandardList',
  component: () => import(path),
});

此时该路由的导入其实是无效的,一定会有bug的.

正确的语法:

//以动态导入路由为例
const path = 'views/StandardList';  //这里有改变
router.addRoute({
  path: '/example/blank/blank',
  name: 'StandardList',
  component: () => import(`@/${path}.vue`),  //这里有改变
});

动态拼接的异步导入总结:

1.最好使用字符串模板.

2.需要保留别名前缀和写死的后缀名,仅中间部分,可以写活.