vue 動態(tài)組件用法示例小結(jié)
本文實例講述了vue 動態(tài)組件用法。分享給大家供大家參考,具體如下:
通過使用保留的 <component> 元素,動態(tài)地綁定到它的 is 特性,我們讓多個組件可以使用同一個掛載點,并動態(tài)切換。根據(jù) v-bind:is="組件名" 中的組件名去自動匹配組件,如果匹配不到則不顯示。
改變掛載的組件,只需要修改is指令的值即可。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 動態(tài)組件</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<button @click='toShow'>點擊顯示子組件</button>
<component v-bind:is="which_to_show"></component>
</div>
<script>
// 創(chuàng)建根實例
new Vue({
el: '#app',
data:{
which_to_show:'first'
},
methods:{
toShow:function(){
var arr = ["first","second","third"];
var index = arr.indexOf(this.which_to_show);
if(index<2){
this.which_to_show = arr[index+1];
}else{
this.which_to_show = arr[0];
}
}
},
components:{
first:{
template:'<div>這是子組件1<div>'
},
second:{
template:'<div>這是子組件2<div>'
},
third:{
template:'<div>這是子組件3<div>'
},
}
})
</script>
</body>
</html>

#keep-alive
動態(tài)切換掉的組件(非當前顯示的組件)是被移除掉了,如果把切換出去的組件保留在內(nèi)存中,可以保留它的狀態(tài)或避免重新渲染。為此可以添加一個 keep-alive 指令參數(shù):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 動態(tài)組件</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<button @click='toShow'>點擊顯示子組件</button>
<!----或者<component v-bind:is="which_to_show" keep-alive></component>也行----->
<keep-alive>
<component v-bind:is="which_to_show" ></component>
</keep-alive>
</div>
<script>
// 創(chuàng)建根實例
new Vue({
el: '#app',
data:{
which_to_show:'first'
},
methods:{
toShow:function(){
var arr = ["first","second","third"];
var index = arr.indexOf(this.which_to_show);
if(index<2){
this.which_to_show = arr[index+1];
}else{
this.which_to_show = arr[0];
} console.log(this.$children);
}
},
components:{
first:{
template:'<div>這是子組件1<div>'
},
second:{
template:'<div>這是子組件2<div>'
},
third:{
template:'<div>這是子組件3<div>'
},
}
})
</script>
</body>
</html>
說明:
初始情況下,vm.$children屬性中只有一個元素(first組件),
點擊按鈕切換后,vm.$children屬性中有兩個元素,
再次切換后,則有三個元素(三個子組件都保留在內(nèi)存中)。
之后無論如何切換,將一直保持有三個元素。
actived鉤子
可以延遲執(zhí)行當前的組件。
具體用法來說,activate是和template、data等屬性平級的一個屬性,形式是一個函數(shù),函數(shù)里默認有一個參數(shù),而這個參數(shù)是一個函數(shù),執(zhí)行這個函數(shù)時,才會切換組件。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 動態(tài)組件</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<button @click='toShow'>點擊顯示子組件</button>
<!----或者<component v-bind:is="which_to_show" keep-alive></component>也行----->
<keep-alive>
<component v-bind:is="which_to_show" ></component>
</keep-alive>
</div>
<script>
// 創(chuàng)建根實例
var vm = new Vue({
el: '#app',
data: {
which_to_show: "first"
},
methods: {
toShow: function () { //切換組件顯示
var arr = ["first", "second", "third", ""];
var index = arr.indexOf(this.which_to_show);
if (index < 2) {
this.which_to_show = arr[index + 1];
} else {
this.which_to_show = arr[0];
}
console.log(this.$children);
}
},
components: {
first: { //第一個子組件
template: "<div>這里是子組件1</div>"
},
second: { //第二個子組件
template: "<div>這里是子組件2,這里是延遲后的內(nèi)容:{{hello}}</div>",
data: function () {
return {
hello: ""
}
},
activated: function (done) { //執(zhí)行這個參數(shù)時,才會切換組件
console.log('hhh')
var self = this;
var startTime = new Date().getTime(); // get the current time
//兩秒后執(zhí)行
while (new Date().getTime() < startTime + 2000){
self.hello='我是延遲后的內(nèi)容';
}
}
},
third: { //第三個子組件
template: "<div>這里是子組件3</div>"
}
}
});
</script>
</body>
</html>

當切換到第二個組件的時候,會先執(zhí)行activated鉤子,會在兩秒后顯示組件2.起到了延遲加載的作用。
希望本文所述對大家vue.js程序設計有所幫助。
- vue3的動態(tài)組件是如何工作的
- 深入了解Vue動態(tài)組件和異步組件
- vue 動態(tài)組件(component :is) 和 dom元素限制(is)用法說明
- Vue兩種組件類型:遞歸組件和動態(tài)組件的用法
- vue學習筆記之動態(tài)組件和v-once指令簡單示例
- VUE 動態(tài)組件的應用案例分析
- Vue 動態(tài)組件components和v-once指令的實現(xiàn)
- Vue動態(tài)組件和異步組件原理詳解
- vue19 組建 Vue.extend component、組件模版、動態(tài)組件 的實例代碼
- Vue動態(tài)組件與異步組件實例詳解
- vue使用動態(tài)組件實現(xiàn)TAB切換效果
相關(guān)文章
深入理解Vue-cli搭建項目后的目錄結(jié)構(gòu)探秘
本篇文章主要介紹了深入理解Vue-cli搭建項目后的目錄結(jié)構(gòu)探秘,具有一定的參考價值,有興趣的可以了解一下2017-07-07
Vue?調(diào)用攝像頭掃描條碼功能實現(xiàn)代碼
本文介紹了如何使用Vue.js和jsQR庫來實現(xiàn)調(diào)用攝像頭并掃描條碼的功能,通過安裝依賴、獲取攝像頭視頻流、解析條碼等步驟,實現(xiàn)了從開始掃描到停止掃描的完整流程,同時,還強調(diào)了瀏覽器兼容性、HTTPS環(huán)境、權(quán)限問題和性能優(yōu)化的重要性,感興趣的朋友一起看看吧2025-03-03
Vue應用中504錯誤(Gateway timeout)的原因與解決方法
在Vue前端應用中遇到504代理錯誤通常是由于請求在到達服務器之前超時,504錯誤表示網(wǎng)關(guān)超時,可能由后端服務響應慢、網(wǎng)絡問題、代理配置錯誤、請求負載過大、前端請求超時設置不當、服務器資源不足或第三方服務問題引起2024-09-09
Vue實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的實現(xiàn)思路
這篇文章主要介紹了Vue實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面,定義路由的時候配置屬性,這里使用needLogin標記訪問頁面是否需要登錄,本文通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-02-02
ElementUI+命名視圖實現(xiàn)復雜頂部和左側(cè)導航欄
本文主要介紹了ElementUI+命名視圖實現(xiàn)復雜頂部和左側(cè)導航欄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04

