vue學(xué)習(xí)記錄之動(dòng)態(tài)組件淺析
動(dòng)態(tài)組件
釋義:運(yùn)行時(shí)在組件之間動(dòng)態(tài)切換的方法。可以將多個(gè)條件組件(使用
v-if、v-else-if、v-else切換的組件)簡(jiǎn)化為一行代碼
看個(gè)例子 一般來(lái)講,我們會(huì)這樣實(shí)現(xiàn)一個(gè)tabs的切換
<template>
<div id="app">
<button v-for="item in tabs" :key="item" @click="onClickTabs(item)">{{item}}</button>
<Buy v-if="index==='Buy'"/>
<cutUp v-else-if="index==='cutUp'"/>
<Fried v-else/>
</div>
</template>
<script>
import Buy from "./components/Buy.vue";
import cutUp from "./components/cutUp.vue";
import Fried from "./components/Fried.vue";
export default {
name: "app",
components: {
Buy,
cutUp,
Fried,
},
created() {},
data() {
return {
tabs: ["Buy", "cutUp", "Fried"],
index:'Buy'
};
},
methods: {
onClickTabs(item){
console.log(item,9999);
this.index=item
}
},
};
</script>
<style lang="scss" scoped>
</style>而動(dòng)態(tài)組件形式則可以寫(xiě)成
<template>
<div id="app">
<button v-for="item in tabs" :key="item" @click="onClickTabs(item)">{{item}}</button>
<component :is="index"></component>
</div>
</template>
<script>
import Buy from "./components/Buy.vue";
import cutUp from "./components/cutUp.vue";
import Fried from "./components/Fried.vue";
export default {
name: "app",
components: {
Buy,
cutUp,
Fried,
},
created() {},
data() {
return {
tabs: ["Buy", "cutUp", "Fried"],
index:'Buy'
};
},
methods: {
onClickTabs(item){
console.log(item,9999);
this.index=item
}
},
};
</script>
<style lang="scss" scoped>
</style>在以上例子中,
v-if加組件名被components加is替換掉了。至于其他,則沒(méi)太多分別。該傳data傳data,該傳狀態(tài)傳狀態(tài)。
補(bǔ)充:動(dòng)態(tài)調(diào)用組件示例
<div id="app">
<button @click="changeComponent('coma')">coma</button>
<button @click="changeComponent('comb')">comb</button>
<button @click="changeComponent('comc')">comc</button>
<component :is="com_name"></component>
</div>
<script>
var coma = {
template: '<div>aaaa</div>'
}
var comb = {
template: '<div>bbbb</div>'
}
var comc = {
template: '<div>cccc</div>'
}
let vm = new Vue({
el: '#app',
data: {
com_name: 'coma'
},
components: {
coma: coma,
comb: comb,
comc: comc,
},
methods: {
changeComponent: function(name) {
this.com_name = name
}
}
})
</script>
總結(jié)
到此這篇關(guān)于vue學(xué)習(xí)記錄之動(dòng)態(tài)組件的文章就介紹到這了,更多相關(guān)vue動(dòng)態(tài)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue頁(yè)面引入three.js實(shí)現(xiàn)3d動(dòng)畫(huà)場(chǎng)景操作
這篇文章主要介紹了vue頁(yè)面引入three.js實(shí)現(xiàn)3d動(dòng)畫(huà)場(chǎng)景操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue-cli5.0?webpack?采用?copy-webpack-plugin?打包復(fù)制文件的方法
今天就好好說(shuō)說(shuō)vue-cli5.0種使用copy-webpack-plugin插件該如何配置的問(wèn)題。這里我們安裝的 copy-webpack-plugin 的版本是 ^11.0.0,感興趣的朋友一起看看吧2022-06-06
在?Vue?項(xiàng)目中如何引用?assets?文件夾中的圖片(方式匯總)
Vue中引用assets文件夾中的圖片有多種方式,在.vue文件的模板部分,使用相對(duì)路徑或通過(guò)@別名引用圖片,在CSS中,通過(guò)相對(duì)路徑或@別名引用圖片作為背景,在JavaScript中,通過(guò)import語(yǔ)句導(dǎo)入圖片資源,并使用v-bind在模板中綁定顯示,這些方法均可有效管理和引用項(xiàng)目中的圖片資源2024-09-09
v-for中動(dòng)態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐
在項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常會(huì)遇到表單保存的功能,本文主要介紹了v-for中動(dòng)態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-05-05

