为什么VUE计算在设置函数中设置因值之后,呼叫获取?

发布于 2025-01-31 01:42:42 字数 1091 浏览 0 评论 0原文

我将计算值定义为波纹

模板:

         <date-picker
            v-model="rangeDate"
            type="date"
            format="jYYYY/jMM/jDD"
            display-format="jYYYY/jMM/jDD"
            input-class="vgt-input"
            :state="errors.length > 0 ? false : null"
            :placeholder="$t('fields.ads.rangeDate')"
            clearable
            range
          />

{{rangedate}}

脚本:

data: () => ({
    body: {}
  ),


computed: {
rangeDate: {
      get() {
        return [this.body.sdate, this.body.fdate]
      },
      set(newVal) {
        // console.log(newVal)
        if (newVal.length === 0) {
          this.body.sdate = ''
          this.body.fdate = ''
        } else if (newVal.length === 1) {
          this.body.sdate = newVal[0]
          this.body.fdate = `${newVal[0]}`
        } else {
          this.body.sdate = newVal[0]
          this.body.fdate = `${newVal[1]}`
        }
      },
    },
},

此值用于范围日历改变。

我认为必须在每个依赖值更改后重新计算计算值,但是当相关值变化时,设置函数不会导致调用get get函数。 请帮我。

I define a computed value as bellow

template :

         <date-picker
            v-model="rangeDate"
            type="date"
            format="jYYYY/jMM/jDD"
            display-format="jYYYY/jMM/jDD"
            input-class="vgt-input"
            :state="errors.length > 0 ? false : null"
            :placeholder="$t('fields.ads.rangeDate')"
            clearable
            range
          />

{{rangeDate}}

script:

data: () => ({
    body: {}
  ),


computed: {
rangeDate: {
      get() {
        return [this.body.sdate, this.body.fdate]
      },
      set(newVal) {
        // console.log(newVal)
        if (newVal.length === 0) {
          this.body.sdate = ''
          this.body.fdate = ''
        } else if (newVal.length === 1) {
          this.body.sdate = newVal[0]
          this.body.fdate = `${newVal[0]}`
        } else {
          this.body.sdate = newVal[0]
          this.body.fdate = `${newVal[1]}`
        }
      },
    },
},

this value is used for range calendar but by setting that through v-model it set the sdate and fdate correctly but in other places of template that show rangeDate won't change.

I thought the computed value must be recalculated after the change of each depended values but when the dependent values change in set function don't cause to call get function.
please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-02-07 01:42:42

我了解,更改对象的价值不会影响提高手表方法。

I understood that changing the value of an object does not affect raising the watch method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文