Vue中使用provide和inject

相信你們在工做中必定遇到過多層嵌套組件,而vue 的組件數據通訊方式又有不少種。css

好比vuex、$parent與$children、prop、$emit與$on、$attrs與$lisenters、eventBus、refvue

今天主要爲你們分享的是provideinjectvuex

不少人會問,那我直接使用vuex不就好了嗎?數組

vuex當然是好!bash

可是,有可能項目自己並無使用vuex的必要,這個時候provideinject就閃亮登場啦~ide

使咱們開發的時候,若有神助~

官方解釋

provide

選項應該是一個對象或返回一個對象的函數。該對象包含可注入其子孫的property函數

inject

能夠是一個字符串數組、也能夠是一個對象ui

說白了,就是provide在祖先組件中注入,inject 在須要使用的地方引入便可。this

咱們能夠把依賴注入看作一部分大範圍的prop,只不過它如下特色:spa

  • 祖先組件不須要知道哪些後代組件使用它提供的屬性

  • 後代組件不須要知道被注入的屬性是來自那裏

注意:provide 和 inject 綁定並非可響應的。這是刻意爲之的。然而,若是你傳入了一個可監聽的對象,那麼其對象的 property 仍是可響應的。

實例

目錄結構

祖先

index.vue

<template>
<div class="grandPa">
  爺爺級別 : <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,   //傳入一個可監聽的對象
    cityFn: () => this.city,  //經過computed來計算注入的值
    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">
  父親級別 : <strong>只用做中轉</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">
  孫子級別 : <strong>{{ nameObj.name }} 今年 <i class="blue">{{ age }}</i>歲, 城市<i class="yellow">{{ city }}</i></strong>
</div>
</template>

<script>
export default {
name: 'ProvideSon',
//inject 來獲取的值
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>


複製代碼

咱們來看一下運行結果。

圖一:未點擊【改變名稱】按鈕,原有狀態

圖二:已經點擊【改變名稱】按鈕,更新後狀態

你們能夠對比一下先後差別。

會發現一個小細節。

不管我點擊多少次,孫子組件的年齡age字段永遠都是12並不會發生變化。

正是官網所提到的provideinject 綁定並非可響應的。這是刻意爲之的。

因此你們使用的時候,必定要注意注入的方式,否則極可能沒法實現數據響應。

但願今天的分享對你能有所幫助~