欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中使用provide和inject實(shí)例對(duì)比分析

 更新時(shí)間:2023年08月08日 10:53:05   作者:刀哥  
這篇文章主要為大家介紹了Vue中使用provide和inject實(shí)例對(duì)比分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

引言

相信大家在工作中一定遇到過(guò)多層嵌套組件,而vue 的組件數(shù)據(jù)通信方式又有很多種。

比如vuex、$parent與$children、prop、$emit與$on、$attrs與$lisenters、eventBus、ref。

今天主要為大家分享的是provideinject。

很多人會(huì)問(wèn),那我直接使用vuex不就行了嗎?

vuex固然是好!

但是,有可能項(xiàng)目本身并沒(méi)有使用vuex的必要,這個(gè)時(shí)候provideinject就閃亮登場(chǎng)啦~

使我們開(kāi)發(fā)的時(shí)候,如有神助~

官方解釋

provide

選項(xiàng)應(yīng)該是一個(gè)對(duì)象或返回一個(gè)對(duì)象的函數(shù)。該對(duì)象包含可注入其子孫的property

inject

可以是一個(gè)字符串?dāng)?shù)組、也可以是一個(gè)對(duì)象

說(shuō)白了,就是provide在祖先組件中注入,inject 在需要使用的地方引入即可。

我們可以把依賴注入看做一部分大范圍的prop,只不過(guò)它以下特點(diǎn):

  • 祖先組件不需要知道哪些后代組件使用它提供的屬性
  • 后代組件不需要知道被注入的屬性是來(lái)自那里

注意:provide 和 inject 綁定并不是可響應(yīng)的。這是刻意為之的。然而,如果你傳入了一個(gè)可監(jiān)聽(tīng)的對(duì)象,那么其對(duì)象的 property 還是可響應(yīng)的。

實(shí)例

目錄結(jié)構(gòu)

祖先

index.vue

<template>
<div class="grandPa">
  爺爺級(jí)別 : <strong>{{ nameObj.name }} 今年 <i class="blue">{{ age }}</i>歲, 城市<i class="yellow">{{ city }}</i></strong>
  <child />
  <br>
  <br>
  <el-button type="primary" plain @click="changeName">改變名稱</el-button>
</div>
</template>
<script>
import child from '@/components/ProvideText/parent'
export default {
name: 'ProvideGrandPa',
components: { child },
data: function() {
  return {
    nameObj: {
      name: '小布'
    },
    age: 12,
    city: '北京'
  }
},
provide() {
  return {
    nameObj: this.nameObj,   //傳入一個(gè)可監(jiān)聽(tīng)的對(duì)象
    cityFn: () => this.city,  //通過(guò)computed來(lái)計(jì)算注入的值
    age: this.age  //直接傳值
  }
},
methods: {
  changeName() {
    if (this.nameObj.name === '小布') {
      this.nameObj.name = '貂蟬'
      this.city = '香港'
      this.age = 24
    } else {
      this.nameObj.name = '小布'
      this.city = '北京'
      this.age = 12
    }
  }
}
}
</script>
<style lang="scss" scoped>
.grandPa{
width: 600px;
height:100px;
line-height: 100px;
border: 2px solid  #7fffd4;
padding:0 10px;
text-align: center;
margin:50px auto;
strong{
  font-size: 20px;
  text-decoration: underline;;
}
.blue{
    color: blue;
}
}
</style>

中間組件

parent.vue

<template>
<div class="parent">
  父親級(jí)別 : <strong>只用作中轉(zhuǎn)</strong>
  <son />
</div>
</template>
<script>
import Son from './son'
export default {
name: 'ProvideParent',
components: { Son }
}
</script>
<style lang="scss" scoped>
.parent{
height:100px;
line-height: 100px;
border: 2px solid  #feafef;
padding:0 10px;
margin-top: 20px;
strong{
  font-size: 20px;
  text-decoration: underline;;
}
}
</style>

后代組件

son.vue

<template>
<div class="son">
  孫子級(jí)別 : <strong>{{ nameObj.name }} 今年 <i class="blue">{{ age }}</i>歲, 城市<i class="yellow">{{ city }}</i></strong>
</div>
</template>
<script>
export default {
name: 'ProvideSon',
//inject 來(lái)獲取的值
inject: ['nameObj', 'age', 'cityFn'],
computed: {
  city() {
    return this.cityFn()
  }
}
}
</script>
<style lang="scss" scoped>
.son{
  height:100px;
  line-height: 100px;
  padding:0 10px;
  margin: 20px;
  border: 1px solid #49e2af;
strong{
  font-size: 20px;
  text-decoration: underline;;
}
.blue{
    color: blue;
}
}
</style>

我們來(lái)看一下運(yùn)行結(jié)果。

圖一:未點(diǎn)擊【改變名稱】按鈕,原有狀態(tài)

圖二:已經(jīng)點(diǎn)擊【改變名稱】按鈕,更新后狀態(tài)

大家可以對(duì)比一下前后差異。

會(huì)發(fā)現(xiàn)一個(gè)小細(xì)節(jié)。

無(wú)論我點(diǎn)擊多少次,孫子組件的年齡age字段永遠(yuǎn)都是12并不會(huì)發(fā)生變化。

正是官網(wǎng)所提到的provide 和 inject 綁定并不是可響應(yīng)的。這是刻意為之的。

所以大家使用的時(shí)候,一定要注意注入的方式,不然很可能無(wú)法實(shí)現(xiàn)數(shù)據(jù)響應(yīng)。

以上就是Vue中使用provide和inject實(shí)例對(duì)比分析的詳細(xì)內(nèi)容,更多關(guān)于Vue使用provide inject對(duì)比的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論