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. 雙擊安裝程序,安裝不了2. python - Pycharm的Debug用不了3. python - 如何用pandas處理分鐘數(shù)據(jù)變成小時(shí)線?4. javascript - 求救!網(wǎng)頁(yè)播放視頻只有聲音沒(méi)有畫(huà)面,網(wǎng)頁(yè)上傳視頻文件時(shí)怎么知道視頻的編碼為H264還是MPEG4??5. css - 請(qǐng)問(wèn)B站頂部的模糊半透明導(dǎo)航條是怎么實(shí)現(xiàn)的呢?6. form表單中的label標(biāo)簽7. pdo - mysql 簡(jiǎn)單注入疑問(wèn)8. windows-7 - Win7中Vmware Workstatoin與Xampp中Apache服務(wù)器端口沖突?9. Python中使用超長(zhǎng)的List導(dǎo)致內(nèi)存占用過(guò)大10. javascript - dropload+tab頁(yè)面,圖文頁(yè)滾動(dòng)有兩個(gè)滾動(dòng)區(qū)域怎么破?
