vue實(shí)現(xiàn)按需加載組件及異步組件功能
說(shuō)實(shí)話(huà),我一開(kāi)始也不知道什么叫按需加載組件,組件還可以按需加載???后來(lái)知道了
學(xué)不完啊...沒(méi)關(guān)系,看我的
按需加載組件,或者異步組件,主要是應(yīng)用了component的 is 屬性
template中的代碼:
這里的每一個(gè)按鈕,都要顯示不同的組件,所以我讓他們使用了同一個(gè)方法名
<template slot-scope="scope"> <el-button type="text" size="mini" @click="handleSchedule('CustomerInfoSchedule', scope.row.customer_id)" >詳情</el-button> <el-button type="text" size="mini" @click="handleSchedule('VisitRecordSchedule', scope.row.customer_id)" >回訪</el-button> <el-button type="text" size="mini" @click="handleSchedule('AddCustomerSchedule',scope.row.customer_id)" >編輯</el-button> <el-button type="text" size="mini" @click="handleSchedule('AddPeopleSchedule', scope.row.customer_id)" >添加聯(lián)系人</el-button> </template> <component :is="currentComponent" :customer_id="customer_id" @componentResult="componentResult" > </component>
script中的代碼:
這里的組件使用request按需引入,我使用的點(diǎn)擊事件,當(dāng)事件觸發(fā)的時(shí)候,引入對(duì)應(yīng)的組件
首先在data中聲明組件的屬性
data() { return { currentComponent: "", customer_id:'', } }
然后注冊(cè)組件
這里的組件作為一個(gè)個(gè)方法,組件名是方法名,組件內(nèi)容是方法體,有幾個(gè)組件就寫(xiě)幾個(gè)方法
components: { AddCustomerSchedule(resolve) { require(["../components/AddCustomer"], resolve); }, AddPeopleSchedule(resolve) { require(["../components/AddPeople"], resolve); }, CustomerInfoSchedule(resolve) { require(["../components/CustomerInfo"], resolve); }, VisitRecordSchedule(resolve) { require(["../components/VisitRecord"], resolve); }, },
定義的方法
// 這里直接接收name,然后相對(duì)應(yīng)的引入組件,同時(shí)傳值 handleSchedule(name, id) { this.customer_id = id; this.currentComponent = name; }, // 這是子組件觸發(fā)父組件返回回來(lái)的方法,因?yàn)槲业慕M件都是彈出框 // 所以在子組件關(guān)閉彈出框的時(shí)候,我讓this.currentComponent為空 // 同時(shí)可以選擇性的刷新數(shù)據(jù) componentResult(type) { if (type == "upData") { this.getTableData(); } else { this.currentComponent = ""; } },
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)按需加載組件及異步組件功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue實(shí)現(xiàn)的多頁(yè)面項(xiàng)目如何優(yōu)化打包的步驟詳解
這篇文章主要介紹了vue實(shí)現(xiàn)的多頁(yè)面項(xiàng)目如何優(yōu)化打包的步驟詳解,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07在使用vuex的時(shí)候出現(xiàn)commit未定義錯(cuò)誤的解決
這篇文章主要介紹了在使用vuex的時(shí)候出現(xiàn)commit未定義錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01使用FileReader API創(chuàng)建Vue文件閱讀器組件
這篇文章主要介紹了使用FileReader API創(chuàng)建一個(gè)Vue的文件閱讀器組件,需要的朋友可以參考下2018-04-04vue?實(shí)現(xiàn)動(dòng)態(tài)設(shè)置元素的高度
這篇文章主要介紹了在vue中實(shí)現(xiàn)動(dòng)態(tài)設(shè)置元素的高度,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue2.0使用swiper組件實(shí)現(xiàn)輪播的示例代碼
下面小編就為大家分享一篇vue2.0使用swiper組件實(shí)現(xiàn)輪播的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue使用video插件vue-video-player詳解
這篇文章主要為大家詳細(xì)介紹了vue使用video插件vue-video-player,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled
最近在做項(xiàng)目的時(shí)候遇到了些問(wèn)題,所以這篇文章主要給大家介紹了關(guān)于Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled的解決方法,需要的朋友可以參考下2023-01-01