“为什么在Vue3 Watch中,oldValue与newValue相同?”

huangapple go评论83阅读模式
英文:

why the oldvalue is same to the new value in the vue3 Watch,

问题

<script setup lang="ts">
import { ref, reactive, watch, watchEffect } from 'vue'

interface address {
  nation: string
}

interface basketballPlayer {
  name: string
  age: number
  address: address
}

let str = ref<string>('jth')
let basketballPlayer = ref<basketballPlayer>({
  name: 'jokic',
  age: 28,
  address: {
    nation: 'Seriba',
  },
})

const handleClick = (): void => {
  str.value = 'Eric'
  basketballPlayer.value.address.nation = 'americal'
}

watch(
  [str, basketballPlayer],
  (newVal, oldVal) => {
    console.log(newVal, oldVal)
  },
  {
    deep: true,
  }
)
</script>

<template>
  <div>
    watch
    <a-button type="primary" @click="handleClick">更改</a-button>
  </div>
</template>

<style scoped></style>

查看图片描述 我定义了一个对象,并且它有一个深层结构。当我点击按钮并修改了地址的国家时,控制台栏会显示两个对象具有相同的数据,旧值与新值相同,为什么?

英文:
    &lt;script setup lang=&quot;ts&quot;&gt;
    import { ref, reactive, watch, watchEffect } from &#39;vue&#39;

    interface address {
      nation: string
    }

    interface basketballPlayer {
      name: string
      age: number
      address: address
    }

    let str = ref&lt;string&gt;(&#39;jth&#39;)
    let basketballPlayer = ref&lt;basketballPlayer&gt;({
      name: &#39;jokic&#39;,
      age: 28,
      address: {
        nation: &#39;Seriba&#39;,
      },
    })

    const handleClick = (): void =&gt; {
      str.value = &#39;Eric&#39;
      basketballPlayer.value.address.nation = &#39;americal&#39;
    }

    watch(
      [str, basketballPlayer],
      (newVal, oldVal) =&gt; {
        console.log(newVal, oldVal)
      },
      {
        // immediate: true,
        deep: true,
      }
    )
    &lt;/script&gt;

    &lt;template&gt;
      &lt;div&gt;
        watch
        &lt;a-button type=&quot;primary&quot; @click=&quot;handleClick&quot;&gt;更改&lt;/a-button&gt;
      &lt;/div&gt;
    &lt;/template&gt;

    &lt;style scoped&gt;&lt;/style&gt;

enter image description here
I define a object,and it has a deep structure,when i clike the button,and modified the address's nation,the console bar shows that,the two object has the same data,old value is same to new value,why

答案1

得分: 1

文档中提到了:

> 注意,在深度模式下,如果回调由深层次的变化触发,新值和旧值将是相同的对象

如果您想看到差异,您必须使用一个getter

watch(
  () =&gt; basketballPlayer.value.address.nation,
  (oldVal, newVal) =&gt; {
    console.log(&#39;oldVal&#39;, oldVal);
    console.log(&#39;newVal&#39;, newVal);
  }
);
英文:

In the doc, they have mentioned it:

> Note in deep mode, the new value and the old will be the same object
> if the callback was triggered by a deep mutation.

If you want to see the difference, you have to use a getter

watch(
  () =&gt; basketballPlayer.value.address.nation,
  (oldVal, newVal) =&gt; {
    console.log(&#39;oldVal&#39;, oldVal);
    console.log(&#39;newVal&#39;, newVal);
  }
);

huangapple
  • 本文由 发表于 2023年6月29日 17:10:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定