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

动态注册组件

方式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
相关推荐
晚霞的不甘1 分钟前
Flutter for OpenHarmony:注入灵魂:购物车的数据驱动与状态管理实战
android·前端·javascript·flutter·前端框架
skywalk81636 分钟前
cbsd的clonos/control-pane web管理页面一直闪烁和网页打开显示500error 的问题解决(500error问题未解决)
服务器·前端·freebsd·cbsd
weixin_436525079 分钟前
若依多租户版 - modules中创建子模块
java·服务器·前端
好奇的菜鸟11 分钟前
使用 Vite 快速创建 React + TypeScript 项目全记录
前端·react.js·typescript
*小雪12 分钟前
nvm的安装与管理和npm audit的报错解决
前端·npm·node.js
Marshmallowc14 分钟前
React useState 数据不同步?深度解析无限滚动中的“闭包陷阱”与异步更新丢失问题
前端·javascript·react.js·闭包·fiber架构
某柚啊15 分钟前
解决 minimatch 类型报错问题
前端·webpack·npm
前端 贾公子17 分钟前
npm 发包配置双重身份验证
前端·javascript·微信小程序·小程序·github
xkxnq27 分钟前
第四阶段:Vue 进阶与生态整合(第 48 天)(Vue 与 Axios 整合:实现 HTTP 请求的封装与拦截)
前端·vue.js·http
CappuccinoRose39 分钟前
React框架学习文档(三)
前端·react.js·ui·数组·标签·属性·jsx