如何观看$ attrs指令,特别是$ attr.value

发布于 2025-02-01 20:38:14 字数 1010 浏览 0 评论 0原文

您如何在VUE中观看$ attr.value

我有一个子组件:

<custom-component v-model="someData"/>

父组件中的某个地方:

this.$emit('input', this.dataToChild)

当我更新this.datatoChild时,在父组件中,子组件中的V模型显示相应的更改。但是,我还希望对子组件中的yemedata进行更改,以便在父组件中反映出this.datatoChild将包含任何yemedata>具有Somedata更改时。我知道此更改反映到this。$ attr.value。要注意更改,我知道我可以在VUE中使用观察者

我的问题是要观看的语法是什么。$ attr.value?我知道要看一个我可以做的道具:

watch: {
   propToWatch: function(newVal, oldVal) {
       this.dataToChild = newVal
   }
}

我也知道要观看一个我们可以的嵌套道具:

watch: {
    'nested.propToWatch': function(newVal, oldVal) {
        this.dataToChild = newVal
    }
}

但是我不确定如何观看$ attr.value,是否只是:

watch: {
    '$attrs.val': function(newVal, oldVal) {
        this.dataToChild = newVal
    }
}

How do you watch $attrs.value in vue?

I have a child component:

<custom-component v-model="someData"/>

Somewhere in the parent component:

this.$emit('input', this.dataToChild)

When I update this.dataToChild in the parent component the v-model in the child component shows the corresponding changes. However I also want changes made to someData in the child component to also reflect in the parent component, such that this.dataToChild will contain whatever someData has when someData changes. I know this change is reflected to this.$attrs.value. To watch for changes I know I can use watchers in vue.

My question is what would be the syntax to watch this.$attrs.value? I know to watch a prop I can just do:

watch: {
   propToWatch: function(newVal, oldVal) {
       this.dataToChild = newVal
   }
}

I also know to watch a nested prop we can:

watch: {
    'nested.propToWatch': function(newVal, oldVal) {
        this.dataToChild = newVal
    }
}

But I'm not sure how to watch $attrs.value, would it just be:

watch: {
    '$attrs.val': function(newVal, oldVal) {
        this.dataToChild = newVal
    }
}

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

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

发布评论

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

评论(1

梦里寻她 2025-02-08 20:38:14

事实证明,就像您将如何使用嵌套道具(即:

watch: {
    '$attrs.value': function(newVal, oldVal) {
        this.dataToChild = newVal
    }
}

Turns out it's just like how you would do it with a nested prop, ie:

watch: {
    '$attrs.value': function(newVal, oldVal) {
        this.dataToChild = newVal
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文