简体   繁体   English

从Vue js中发出的子组件方法中更改父属性

[英]Change parent property from within emitted child component method in Vue js

Inside vue component I have another child component datatabel which has emitted method sortdatatabel组件内部,我有另一个子组件数据表,它发出了方法sort

<datatable @sort="sortBy"></datatable>

Currently the emitted sort method calls another method sortBy in parent component, which then updates a property in the parent.当前发出的sort方法调用父组件中的另一个方法sortBy ,然后更新父组件中的属性。

Is it possible to update the parent property directly form emitted sort method?是否可以直接从发出的sort方法更新父属性?

Something like this:像这样的东西:

<datatable @sort="parentAttribute = 'value that was emitted'"></datatable>

The above example doesn't work, but I am not probably doing it right.上面的例子不起作用,但我可能做得不对。

Can that be done, if so, what would be the right syntax?可以这样做吗,如果可以,正确的语法是什么?

Yes you can access the event payload with $event是的,您可以使用$event访问事件有效负载

<datatable @sort="parentAttribute = $event"/>

https://v2.vuejs.org/v2/guide/components-custom-events.html https://v2.vuejs.org/v2/guide/components-custom-events.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue.js 中的子组件未向父组件发出数据 - Data not emitted from child to parent component in Vue.js 发出的事件不会调用 Vue JS 组件中的父方法 - Emitted event doesn't call parent method in Vue JS component Vue 父组件无法捕获子组件发出的事件 - Vue parent component unable to catch event emitted from the child 在Vue.js中从父组件执行子方法 - Executing child method from parent component in Vue.js 在Vue.JS中的父组件中创建子组件 - Creating a child component within a parent component in Vue.JS Vue:单击按钮时从父组件更改子组件的数据无法设置未定义的属性 - Vue: change data of child component from parent component on button click Cannot set property of undefined 如何在父组件中使用来自 Vue.js 子组件的数据? - How do I use data from Vue.js child component within parent component? 用 Jest 测试 Vue:由父组件处理的自定义事件,由子组件发出 - Testing Vue with Jest: custom event handled by parent, emitted by child component Vue.js子组件和父组件之间未发出事件 - Vue.js no event emitted between child and parent components 如何处理自定义子事件,该事件通过this。$ emit在父组件中<script> portion of the .vue file? - How can I handle a custom child event, emitted via this.$emit, in a parent component, within the <script> portion of the .vue file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM