javascript - vue使用異步組件結(jié)合is按需加載組件的問(wèn)題
問(wèn)題描述
<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) })} } }
點(diǎn)擊的時(shí)候去加載異步組件(可以載入組件),但報(bào)下面的錯(cuò)
[Vue warn]: Unknown custom element: <abc> - did you register the component correctly? For recursive components, make sure to provide the 'name' option.
嘗試為abc組件加上name還是報(bào)這樣的錯(cuò),有人知道怎么解決嗎?abc.vue
export default { name: ’abc’,}
問(wèn)題解答
回答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})
然后在點(diǎn)擊的時(shí)候把flag設(shè)置為true就解決了那個(gè)報(bào)錯(cuò)問(wèn)題
回答2:我是用WEBPACK解決的。可以參看我的項(xiàng)目。
相關(guān)文章:
1. mysql - 數(shù)據(jù)庫(kù)建字段,默認(rèn)值空和empty string有什么區(qū)別 1102. 新人求教MySQL關(guān)于判斷后拼接條件進(jìn)行查詢(xún)的sql語(yǔ)句3. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語(yǔ)法實(shí)現(xiàn)存在即更新應(yīng)該使用哪個(gè)標(biāo)簽?4. mysql - 這種分級(jí)一對(duì)多,且分級(jí)不平衡的模型該怎么設(shè)計(jì)表?5. Navicat for mysql 中以json格式儲(chǔ)存的數(shù)據(jù)存在大量反斜杠,如何去除?6. mysql - 數(shù)據(jù)庫(kù)表中,兩個(gè)表互為外鍵參考如何解決7. php - 公眾號(hào)文章底部的小程序二維碼如何統(tǒng)計(jì)?8. mysql - 表名稱(chēng)前綴到底有啥用?9. mysql - 千萬(wàn)數(shù)據(jù) 分頁(yè),當(dāng)偏移量 原來(lái)越大時(shí),怎么優(yōu)化速度10. mac OSX10.12.4 (16E195)下Mysql 5.7.18找不到配置文件my.cnf
