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

动态注册组件

方式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. Vue Router 动态缓存组件

    2024-02-03 07:30:03       15 阅读
  2. 6-动态

    2024-02-03 07:30:03       10 阅读
  3. 6.基础-动态

    2024-02-03 07:30:03       23 阅读
  4. 前端根据权限生成三级

    2024-02-03 07:30:03       5 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 07:30:03       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 07:30:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 07:30:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 07:30:03       18 阅读

热门阅读

  1. 【conda】容易遗忘的命令使用总结

    2024-02-03 07:30:03       35 阅读
  2. 微信小程序如何控制元素的显示和隐藏

    2024-02-03 07:30:03       25 阅读
  3. Acwing---2816. 判断子序列

    2024-02-03 07:30:03       27 阅读
  4. Go语言中...(三个点)的使用几个常见情况

    2024-02-03 07:30:03       26 阅读
  5. qt编程---->qml

    2024-02-03 07:30:03       29 阅读
  6. node版本对应的npm版本

    2024-02-03 07:30:03       30 阅读
  7. PyQt子线程处理业务事件

    2024-02-03 07:30:03       31 阅读