溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在vue中實(shí)現(xiàn)一個動態(tài)子組件

發(fā)布時間:2021-04-19 17:37:36 來源:億速云 閱讀:325 作者:Leah 欄目:web開發(fā)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在vue中實(shí)現(xiàn)一個動態(tài)子組件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

vue是什么軟件

Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫只關(guān)注視圖層,方便與第三方庫和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復(fù)雜的單頁應(yīng)用。

方式一:局部注冊所需組件

<div id="example">
 <button @click="change">切換頁面</button>
 <component :is="currentView"></component>
</div>

<script>
var home = {template:'<div>我是主頁</div>'};
var post = {template:'<div>我是提交頁</div>'};
var archive = {template:'<div>我是存檔頁</div>'};
new Vue({
 el: '#example',
 components: {
  home,
  post,
  archive,
 },
 data:{
  index:0,
  arr:['home','post','archive'],
 },
 computed:{
  currentView(){
    return this.arr[this.index];
  }
 },
 methods:{
  change(){
   this.index = (++this.index)%3;
  }
 }
})
</script>

使用<keep-alive>緩存

<keep-alive> 包裹動態(tài)組件時,會緩存不活動的組件實(shí)例,而不是銷毀它們。和 <transition>相似,<keep-alive> 是一個抽象組件:它自身不會渲染一個 DOM 元素,也不會出現(xiàn)在父組件鏈中。

基本用法:

<div id="example">
 <button @click="change">切換頁面</button>
 <keep-alive>
  <component :is="currentView"></component> 
 </keep-alive>
</div>

條件判斷

如果有多個條件性的子元素,<keep-alive> 要求同時只有一個子元素被渲染:

<div id="example">
 <button @click="change">切換頁面</button>
 <keep-alive>
  <home v-if="index===0"></home>
  <posts v-else-if="index===1"></posts>
  <archive v-else></archive> 
 </keep-alive>
</div>

<script>
new Vue({
 el: '#example',
 components:{
  home:{template:`<div>我是主頁</div>`},
  posts:{template:`<div>我是提交頁</div>`},
  archive:{template:`<div>我是存檔頁</div>`},
 },
 data:{
  index:0,
 },
 methods:{
  change(){
   let len = Object.keys(this.$options.components).length;
   this.index = (++this.index)%len;
  }
 }
})
</script>

activated 和 deactivated

activated 和 deactivated 在 <keep-alive> 樹內(nèi)的所有嵌套組件中觸發(fā):

<div id="example">
 <button @click="change">切換頁面</button>
 <keep-alive>
  <component :is="currentView" @pass-data="getData"></component> 
 </keep-alive>
 <p>{{msg}}</p>
</div>

<script>
new Vue({
 el: '#example',
 data:{
  index:0,
  msg:'',  
  arr:[
   { 
    template:`<div>我是主頁</div>`,
    activated(){
     this.$emit('pass-data','主頁被添加');
    },
    deactivated(){
     this.$emit('pass-data','主頁被移除');
    },    
   },
   {template:`<div>我是提交頁</div>`},
   {template:`<div>我是存檔頁</div>`}
  ],
 },
 computed:{
  currentView(){
    return this.arr[this.index];
  }
 },
 methods:{
  change(){
   var len = this.arr.length;
   this.index = (++this.index)% len;
  },
  getData(value){
   this.msg = value;
   setTimeout(()=>{
    this.msg = '';
   },500)
  }
 }
})
</script>

include和exclude

include 和exclude屬性允許組件有條件地緩存。二者都可以用逗號分隔字符串、正則表達(dá)式或一個數(shù)組來表示:

<!-- 逗號分隔字符串 -->
<keep-alive include="a,b">
 <component :is="view"></component>
</keep-alive>
<!-- 正則表達(dá)式 (使用 v-bind) -->
<keep-alive :include="/a|b/">
 <component :is="view"></component>
</keep-alive>
<!-- Array (use v-bind) -->
<keep-alive :include="['a', 'b']">
 <component :is="view"></component>
</keep-alive>

匹配首先檢查組件自身name選項(xiàng),如果name選項(xiàng)不可用,則匹配它的局部注冊名稱(父組件 components 選項(xiàng)的鍵值)。匿名組件不能被匹配。

<keep-alive include="home,archive">
  <component :is="currentView"></component> 
 </keep-alive>

上面的代碼,表示只緩存home和archive,不緩存posts

方式二:動態(tài)注冊組件實(shí)現(xiàn)

 asyncComponents(templateName){
  this.curNavComponents = require(`./components/${templateName}.vue`).default;
}

上述就是小編為大家分享的怎么在vue中實(shí)現(xiàn)一個動態(tài)子組件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI