Vue.js中provide/inject實現(xiàn)響應(yīng)式數(shù)據(jù)更新的方法示例
vue.js官方文檔:https://cn.vuejs.org/v2/api/#provide-inject
首先假設(shè)我們在祖輩時候傳入進來是個動態(tài)的數(shù)據(jù),官方不是說如果你傳入了一個可監(jiān)聽的對象,那么其對象還是可響應(yīng)的么?
parent父頁面:
export default {
provide() {
return { foo: this.fonnB }
},
data(){
return { fonnB: 'old word '}
}
created() {
setTimeout(()=>{
this.fonnB = 'new words'; // 這里僅僅foonB變化了,foo沒有變化
this._provided.foo="new words"; // 這里foo變化了,但子組件獲得的foo 依舊是old words
console.log( this._provided);
},1000)
},
}
child子頁面:
export default {
inject:['foo'],
data(){
return { chilrfoo: this.foo }
},
created() {
setTimeout(()=>{
// 子組件獲得的foo 依舊是old words
console.log( this.foo)
},2000)
}
}
結(jié)果:
通過上面方式,經(jīng)過驗證,子組件頁面都沒辦法實現(xiàn)響應(yīng)更新this.foo的值??赡芪覀儗俜嚼斫膺€是有誤,下面通過網(wǎng)上資料和自己構(gòu)思實現(xiàn)了響應(yīng)式數(shù)據(jù)更新
示例(結(jié)果仍不可行)
很明顯上面再父組件定時器內(nèi)我們是改變了數(shù)據(jù)源,這個時候我們就在想,我們改變的數(shù)據(jù)到底有沒有傳入到子孫組件中,那么要驗證這個問題,我們不妨可以在子孫組件中手動寫set 函數(shù),computed 本身就只相當(dāng)于一個get函數(shù),當(dāng)然,你也可以試試watch
parent父頁面:
export default {
provide() {
return { foo: this.fonnB }
},
data(){
return {
fonnB: 'old word'
}
}
created() {
setTimeout(()=>{
this.fonnB = "new words";
// 這里foo變化了,但子組件獲得的foo 依舊是old words
},1000)
},
}
child子頁面:
export default {
inject:['foo'],
data(){
return {
childfooOld: this.foo
}
},
computed:{
chilrdfoo() {
return this.foo
}
},
created () {
console.log(this.foo)
// -> 'old word'
setTimeout(() => {
console.log(this.chilrdfoo); // 這里計算屬性依舊是old words
}, 2000);
}
}
通過computed,我們都知道data中有g(shù)et/set,數(shù)據(jù)也是響應(yīng)式的,但為什么沒更新,有點疑惑,如果有大佬知道能解釋清楚的可以探討。
但是,但是,但是!實際需求肯定沒有這么簡單,往往我們需要的是共享父組件里面的動態(tài)數(shù)據(jù),這些數(shù)據(jù)可能來自于data 或者 store。 就是說父組件里面的數(shù)據(jù)發(fā)生變化之后,需要同步到子孫組件里面。這時候該怎么做呢?
我想的是將一個函數(shù)賦值給provide的一個值,這個函數(shù)返回父組件的動態(tài)數(shù)據(jù),然后在子孫組件里面調(diào)用這個函數(shù)。
實際上這個函數(shù)存儲了父組件實例的引用,所以每次子組件都能獲取到最新的數(shù)據(jù)。代碼長下面的樣子:
Parent組件:
<template>
<div class="parent-container">
Parent組件
<br/>
<button type="button" @click="changeName">改變name</button>
<br/>
Parent組件中 name的值: {{name}}
<Child v-bind="{name: 'k3vvvv'}" />
</div>
</template>
<style scoped>
.parent-container {
padding: 30px;
border: 1px solid burlywood;
}
</style>
<script>
import Child from './Child'
export default {
name: 'Parent',
data () {
return {
name: 'Kevin'
}
},
methods: {
changeName (val) {
this.name = 'Kev'
}
},
provide: function () {
return {
nameFromParent: this.name,
getReaciveNameFromParent: () => this.name
}
},
// provide: {
// nameFromParent: this.name,
// getReaciveNameFromParent: () => this.name
// },
components: {
Child
}
}
</script>
Child組件
<template>
<div class="child-container">
Child組件
<br/>
<GrandSon />
</div>
</template>
<style scoped>
.child-container {
padding: 30px;
border: 1px solid burlywood;
}
</style>
<script>
import GrandSon from './GrandSon'
export default {
components: {
GrandSon
}
}
</script>
GrandSon組件:
<template>
<div class="grandson-container">
Grandson組件
<br/>
{{nameFromParent}}
<br/>
{{reactiveNameFromParent}}
</div>
</template>
<style scoped>
.grandson-container {
padding: 30px;
border: 1px solid burlywood;
}
</style>
<script>
export default {
inject: ['nameFromParent', 'getReaciveNameFromParent'],
computed: {
reactiveNameFromParent () {
return this.getReaciveNameFromParent()
}
},
watch: {
'reactiveNameFromParent': function (val) {
console.log('來自Parent組件的name值發(fā)生了變化', val)
}
},
mounted () {
console.log(this.nameFromParent, 'nameFromParent')
}
}
</script>
結(jié)果:
來自于reactiveNameFromParent ,隨著祖先組件變化而變化了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實現(xiàn)編輯器鍵盤抬起時內(nèi)容跟隨光標(biāo)距頂位置向上滾動效果
這篇文章主要介紹了vue實現(xiàn)編輯器鍵盤抬起時內(nèi)容跟隨光標(biāo)距頂位置向上滾動效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
vue tab滾動到一定高度,固定在頂部,點擊tab切換不同的內(nèi)容操作
這篇文章主要介紹了vue tab滾動到一定高度,固定在頂部,點擊tab切換不同的內(nèi)容操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

