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

动态注册组件

方式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
相关推荐
何何____12 分钟前
js常见继承方法详解
前端·javascript
parade岁月17 分钟前
Tailwind CSS 加入 Shopify,正在用 Tailwind 的项目要不要调整?
前端
梨想橙汁29 分钟前
Vue2 快速入门:环境搭建、模板语法、指令系统全解
前端·vue.js
King of fraud34 分钟前
HTML 页面的 CSS 选择器详解
前端·css·html
lichenyang45335 分钟前
ASCF 元服务如何区分“后台恢复”与“携参拉起”:后台标记 + 参数指纹方案
前端
前端 贾公子36 分钟前
Milvus使用指南 (下)
java·服务器·前端
用户2462035827811 小时前
TypeScript类型建模与前后端错误码协议:手机号绑定场景的工程实践
前端
风骏时光牛马1 小时前
云原生架构设计:弹性底座驱动业务持续迭代
前端
用户54277848515401 小时前
浏览器后台休眠节流
前端
蜡台1 小时前
Vue 3 原生 ESM 开发实战:不用打包工具,从零搭建组件化应用
前端·javascript·vue.js·html