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. css3 - [CSS] 動畫效果 3D翻轉bug2. MySQL客戶端吃掉了SQL注解?3. 求大神幫我看看是哪里寫錯了 感謝細心解答4. javascript - JS設置Video視頻對象的currentTime時出現了問題,IE,Edge,火狐,都可以設置,反而chrom卻...5. python - Django分頁和查詢參數的問題6. javascript - 百度echarts series數據更新問題7. javascript - 圖片能在網站顯示,但控制臺仍舊報錯403 (Forbidden)8. python小白的基礎問題 關于while循環的嵌套9. phpstady在win10上運行10. php自學從哪里開始?
