根据路由动态注册组件失败

动态注册组件

方式1 import

这种跟webpack的版本有关系 import低版本不支持传入动态参数

复制代码
<template>
        <components :is="componentName" v-show="isShow" :key="componentName"></components>
</template>


const _import = file => () => import(/* webpackChunkName: `[request][index]` */ `@/pages${file}/index.vue`);

let async = _import(path);
let timer = setTimeout(() => {
    async().then(com => {
  
        Vue.component(name, com.default);
        this.componentName = name;
        this.prevPath = path;
        this.isShow = true
    },
    errors => {
        this.componentName = Error;
        this.$message.error(
            `模块地址加载失败,地址:${path},具体错误:${errors}`
        );
        console.error(errors);
    });
})

方式2 require

这种方式能引入组件成功,并且能打印出com.default。但是刷新页面的时候会出现报错(可能是各种插件的版本导致)

复制代码
<template>
        <components :is="componentName" v-show="isShow" :key="componentName"></components>
</template>



let timer = setTimeout(() => {
    require([`@/views${path}/index.vue`], (com) => {
          Vue.component(name, com.default)
           this.componentName = name;
       },(errors)=>{
         this.$message.error(
               `模块地址加载失败,地址:${path},具体错误:${errors}`
           );
           console.error(errors);
       })
})

解决:替换Vue.component()

复制代码
 Vue.component(name, com.default)
 替换成
this.$options.components[name] = com.default
相关推荐
接着奏乐接着舞2 分钟前
react hooks
前端·javascript·react.js
踢球的打工仔2 分钟前
前端html(3)
前端·算法·html
IDOlaoluo3 分钟前
nginx-sticky怎么用 Nginx 负载均衡添加 sticky 模块完整步骤
前端·chrome
接着奏乐接着舞4 分钟前
react redux 分组
前端·javascript·react.js
IT_陈寒8 分钟前
Vue 3.4 性能优化揭秘:这5个Composition API技巧让我的应用提速40%
前端·人工智能·后端
行走的陀螺仪10 分钟前
实时通信底层原理深度剖析:短轮询、长轮询与WebSocket的本质差异
前端·网络·websocket·网络协议
大猩猩X11 分钟前
vue vxe-gantt 甘特图实现产品进度列表,自定义任务条样式和提示信息
前端·javascript·甘特图·vxe-ui·vxe-gantt
一字白首13 分钟前
Vue 进阶,生命周期 + 工程化开发
前端·javascript·vue.js
沐风。5620 分钟前
css函数
前端·css·css3
tangdou36909865521 分钟前
AI真好玩系列-WebGL爱心粒子手势互动教程 | Interactive Heart Particles with Hand Gestures
前端·人工智能·webgl