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

动态注册组件

方式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
相关推荐
IT_陈寒10 分钟前
Redis的SETNX并发问题让我加了三天班
前端·人工智能·后端
demo007x26 分钟前
Docling 文档转换以及技术架构分析
前端·后端·程序员
京东云开发者1 小时前
京东市民服务又“上新”!这次是黑龙江“龙易办”
前端
袋鱼不重2 小时前
我的神奇同事,AI 用多了居然写了个 Open In Codex
前端·后端·ai编程
Fireworks2 小时前
深入vue3源码解读 -- 1、响应式的基础概念
前端
程序员黑豆2 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
hunterandroid2 小时前
文件存储:内部存储与外部存储
前端
NorBugs3 小时前
飞机大战 Low 版 (Made in AI)
前端
angerdream3 小时前
Android手把手编写儿童手机远程监控App之agentweb如何实现全屏
前端
星栈3 小时前
10 分钟跑起第一个 Dioxus 应用:`dx` CLI、`rsx!` 和热更新好不好用
前端·rust·前端框架