javascript - vue使用异步组件结合is按需加载组件的问题
问题描述
<el-tabs v-model='activeName' @tab-click='handleClick' type='border-card'> <el-tab-pane v-for='item in menu' :label='item.name' :name='item.name' :key='item' :component='item.component'><component :is='item.component'></component></el-tab-pane> </el-tabs>
methods: { handleClick (tab, event) {// 异步加载组件let getCompoentIndex = this.menu.findIndex(x => x.name === tab.name)let component = this.menu[getCompoentIndex].componentif (!this.menu[getCompoentIndex].loading) { this.menu[getCompoentIndex].loading = true Vue.component(component, function (resolve, reject) { setTimeout(function () { require([`./${component}.vue`], resolve)//比如 abc.vue }, 1000) })} } }
点击的时候去加载异步组件(可以载入组件),但报下面的错
[Vue warn]: Unknown custom element: <abc> - did you register the component correctly? For recursive components, make sure to provide the 'name' option.
尝试为abc组件加上name还是报这样的错,有人知道怎么解决吗?abc.vue
export default { name: ’abc’,}
问题解答
回答1:找出了方法就是加上if判断
<el-tab-pane v-for='item in menu' :label='item.name' :name='item.name' :key='item' :component='item.component'><component :is='item.component' v-if=’flag’></component></el-tab-pane>
data:()=>({ flag: false})
然后在点击的时候把flag设置为true就解决了那个报错问题
回答2:我是用WEBPACK解决的。可以参看我的项目。
相关文章:
1. 关于docker下的nginx压力测试2. html5 - node静态资源服务器设置了Cache-Control,但浏览器从来不走3043. docker start -a dockername 老是卡住,什么情况?4. 为什么我ping不通我的docker容器呢???5. docker镜像push报错6. nignx - docker内nginx 80端口被占用7. docker安装后出现Cannot connect to the Docker daemon.8. docker内创建jenkins访问另一个容器下的服务器问题9. dockerfile - 为什么docker容器启动不了?10. debian - docker依赖的aufs-tools源码哪里可以找到啊?

网公网安备