vue動(dòng)態(tài)注冊(cè)組件實(shí)例代碼詳解
寫本篇文章之前其實(shí)也關(guān)注過vue中的一個(gè)關(guān)于加載動(dòng)態(tài)組件is的API,最開始研究它只是用來實(shí)現(xiàn)一個(gè)tab切換的功能,使用起來也蠻不錯(cuò)的。
is
預(yù)期:string | Object (組件的選項(xiàng)對(duì)象)
用于動(dòng)態(tài)組件且基于 DOM 內(nèi)模板的限制來工作。
示例:
<!-- 當(dāng) `currentView` 改變時(shí),組件也跟著改變 --> <component v-bind:is="currentView"></component>
至于用在tab切換中,大概就是:
<template>
<div>
<div>#動(dòng)態(tài)組件實(shí)現(xiàn)tab切換效果#</div><br><br><br>
<nav>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="toggleTabs(first);">{{first}}</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="toggleTabs(second);">{{second}}</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="toggleTabs(third);">{{third}}</a>
</nav>
<first :is="currentView" keep-alive></first>
</div>
</template>
<script>
import first from 'components/first';
import second from 'components/second';
import third from 'components/third';
export default {
data () {
return {
first: "first",
second: "second",
third: "third",
currentView: 'first',
};
},
components: {
first,
second,
third
},
methods: {
toggleTabs (tabText) {
this.currentView = tabText;
}
}
}
</script>
但是今天,一個(gè)前端同行在群里問我“如果當(dāng)前頁(yè)面是根據(jù)傳進(jìn)來的參數(shù)的不同而顯示不同的組件,而且當(dāng)前頁(yè)面中可能會(huì)import進(jìn)來幾十個(gè)子組件,而我又不想挨個(gè)去import這些組件,同時(shí)這些組件又是按需加載的,該咋實(shí)現(xiàn)?” 說實(shí)話,一開始我也懵了。
我在想,實(shí)在不行就用const demo = () => import ( './demo.vue'),或者在組件的components:中按需引入:
components: {
demo: () => import ( './demo.vue')
}
但是我一想,也不對(duì)啊,這樣雖然能實(shí)現(xiàn)按需加載,但是還是要挨個(gè)import這些組件,還是沒有解決實(shí)際的問題。
經(jīng)過查閱資料發(fā)現(xiàn),vue有一個(gè)extend的方法可以實(shí)現(xiàn)。那么這個(gè)extend方法到底是干嘛的?
Vue.extend( options )
Vue.extend 返回的是一個(gè)“擴(kuò)展實(shí)例構(gòu)造器”,也就是預(yù)設(shè)了部分選項(xiàng)的Vue實(shí)例構(gòu)造器。經(jīng)常服務(wù)于Vue.component用來生成組件,可以簡(jiǎn)單理解為當(dāng)在模板中遇到該組件名稱作為標(biāo)簽的自定義元素時(shí),會(huì)自動(dòng)調(diào)用“擴(kuò)展實(shí)例構(gòu)造器”來生成組件實(shí)例,并掛載到自定義元素上。
只是,extend創(chuàng)建的是一個(gè)組件構(gòu)造器,而不是一個(gè)具體的組件實(shí)例,所以他不能直接在new Vue中這樣使用。
使用Vue.extend創(chuàng)建的組件構(gòu)造器最終是可以通過Vue.components注冊(cè)成全局組件或new實(shí)例化后注冊(cè)為局部組件。
接下來就來實(shí)現(xiàn)以下使用Vue.extend和Vue.components注冊(cè)全局組件:
import Vue from 'vue';
const globalComponent = Vue.extend({
template:"<p><a :href='url'>{{nama}}</a></p>",
data:function(){
return{
nama:'某度',
url:'http://www.moudu.com'
}
}
});
Vue.component('globalComponent', globalComponent);
使用這個(gè)全局注冊(cè)的組件:
<template> <globalComponent /> </template>
注冊(cè)全局組件還是很簡(jiǎn)單的,接下來就來實(shí)現(xiàn)根據(jù)傳參的不同加載不同組件的方法:
<template>
<button type="button" @click="toggle('test')">動(dòng)態(tài)注冊(cè)組件<button>
<p><div ref="currentView"></div>
</template>
<script>
import Vue from 'vue'
export default {
data(){
return {}
},
methods: {
toggle(componentName){
this.registerComponent(componentName).then(Component => {
// new Component().$mount(this.$refs.currentView)
new Component({
el: this.$refs.currentView
})
})
},
registerComponent(componentName) {
return import(`@/views/${componentName}.vue`).then(component => {
return Vue.extend(component.default);
});
}
},
}
</script>
這樣,我們就可以根據(jù)動(dòng)態(tài)傳入的參數(shù),通過import( @/views/${componentName}.vue )來加載不同的組件,注意import有一個(gè)回調(diào)的方法,在該回調(diào)中就可以使用 Vue.extend(component.default)來創(chuàng)建一個(gè)組件的構(gòu)造器,然后通過new關(guān)鍵字就可以實(shí)現(xiàn)局部注冊(cè)組件了。
總結(jié)
以上所述是小編給大家介紹的vue動(dòng)態(tài)注冊(cè)組件實(shí)例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
vue中設(shè)置height:100%無(wú)效的問題及解決方法
這篇文章主要介紹了vue中設(shè)置height 100%無(wú)效的問題及解決方法,需要的朋友可以參考下2018-07-07
在vue項(xiàng)目創(chuàng)建的后初始化首次使用stylus安裝方法分享
下面小編就為大家分享一篇在vue項(xiàng)目創(chuàng)建的后初始化首次使用stylus安裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Vue3?Axios攔截器封裝成request文件的示例詳解
這篇文章主要介紹了Vue3?Axios攔截器封裝成request文件,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
vue 解決循環(huán)引用組件報(bào)錯(cuò)的問題
今天小編就為大家分享一篇vue 解決循環(huán)引用組件報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue transition實(shí)現(xiàn)點(diǎn)贊動(dòng)畫效果的示例
點(diǎn)贊動(dòng)畫是網(wǎng)頁(yè)評(píng)論中常見的功能,本文將介紹如何用vue實(shí)現(xiàn)這一效果。點(diǎn)贊時(shí)愛心縮小變大,變大時(shí)略微大一點(diǎn)再變正常,取消點(diǎn)贊時(shí)愛心無(wú)動(dòng)畫,同時(shí)數(shù)字滾動(dòng),+1 時(shí)向上滾動(dòng),-1 時(shí)向下滾動(dòng)2021-05-05
Vue中Keep-Alive緩存組件使用語(yǔ)法及原理深度解析
keep-alive是vue中的內(nèi)置組件,能在組件切換過程中將狀態(tài)保留在內(nèi)存中,防止重復(fù)渲染DOM,這篇文章主要介紹了Vue中Keep-Alive緩存組件使用語(yǔ)法及原理,需要的朋友可以參考下2024-07-07

