溫馨提示×

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

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

Vue怎么實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)更新

發(fā)布時(shí)間:2022-11-23 09:18:37 來源:億速云 閱讀:128 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Vue怎么實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)更新”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Vue怎么實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)更新”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。

首先假設(shè)我們?cè)谧孑厱r(shí)候傳入進(jìn)來是個(gè)動(dòng)態(tài)的數(shù)據(jù),官方不是說如果你傳入了一個(gè)可監(jiān)聽的對(duì)象,那么其對(duì)象還是可響應(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)過驗(yàn)證,子組件頁面都沒辦法實(shí)現(xiàn)響應(yīng)更新this.foo的值。可能我們對(duì)官方理解還是有誤,下面通過網(wǎng)上資料和自己構(gòu)思實(shí)現(xiàn)了響應(yīng)式數(shù)據(jù)更新

示例(結(jié)果仍不可行)

很明顯上面再父組件定時(shí)器內(nèi)我們是改變了數(shù)據(jù)源,這個(gè)時(shí)候我們就在想,我們改變的數(shù)據(jù)到底有沒有傳入到子孫組件中,那么要驗(yàn)證這個(gè)問題,我們不妨可以在子孫組件中手動(dòng)寫set 函數(shù),computed 本身就只相當(dāng)于一個(gè)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); // 這里計(jì)算屬性依舊是old words
    }, 2000);
   }
 }

通過computed,我們都知道data中有g(shù)et/set,數(shù)據(jù)也是響應(yīng)式的,但為什么沒更新,有點(diǎn)疑惑,如果有大佬知道能解釋清楚的可以探討。

但是,但是,但是!實(shí)際需求肯定沒有這么簡(jiǎn)單,往往我們需要的是共享父組件里面的動(dòng)態(tài)數(shù)據(jù),這些數(shù)據(jù)可能來自于data 或者 store。 就是說父組件里面的數(shù)據(jù)發(fā)生變化之后,需要同步到子孫組件里面。這時(shí)候該怎么做呢?
我想的是將一個(gè)函數(shù)賦值給provide的一個(gè)值,這個(gè)函數(shù)返回父組件的動(dòng)態(tài)數(shù)據(jù),然后在子孫組件里面調(diào)用這個(gè)函數(shù)。
實(shí)際上這個(gè)函數(shù)存儲(chǔ)了父組件實(shí)例的引用,所以每次子組件都能獲取到最新的數(shù)據(jù)。代碼長(zhǎng)下面的樣子:

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>

為什么要使用Vue

Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護(hù)性和可測(cè)試性更強(qiáng)的代碼庫,Vue允許可以將一個(gè)網(wǎng)頁分割成可復(fù)用的組件,每個(gè)組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網(wǎng)頁中相應(yīng)的地方,所以越來越多的前端開發(fā)者使用vue。

讀到這里,這篇“Vue怎么實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)更新”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

vue
AI