简体   繁体   English

Vue.js:将数据从一个子组件传递到另一个具有相同父组件的组件?

[英]Vue.js: pass data from one child component to another with the same parent?

I have 2 components, both sharing a common parent.我有 2 个组件,它们共享一个共同的父级。 I want to be able to pass data from one child component to the other.我希望能够将数据从一个子组件传递到另一个。 (I am using vueJS 2) (我正在使用 vueJS 2)

The communication should be handled by the parent component as follows:通信应由parent组件处理如下:

  1. Child 1 sends the data to parent孩子 1 将数据发送给父母
this.$emit('myVarChanged', this.myVar);
  1. The parent listens to the emits from the last, saves the new value, and pass it to the second child:父级侦听最后一个发出的值,保存新值,并将其传递给第二个子级:
<ChildOneComponent @myVarChanged="updateMyVar" />
data() { return { myVar:null } }
methods: {
     updateMyVar(val){
          this.myVar = val;
     }
}
<ChildTwoComponent :myVar="myVar" />
  1. Child 2 gets the new value as props : Child 2 获得新值作为props
props: ['myVar']

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

相关问题 如何在 Vue.js 中将数据从父组件传递到子组件? - How can i pass data from parent component to a child component in Vue.js? Vue.js将刚刚创建的数据从子组件传递到父组件 - Vue.js Pass just created data from child to parent component Vue.js - 使用发射但没有按钮将数据从子组件传递到父组件 - Vue.js - Pass data from child component to parent using emit but without button Vue.js从子组件访问父/根数据 - Vue.js access parent/root data from a child component Vue.js 中的子组件未向父组件发出数据 - Data not emitted from child to parent component in Vue.js 如何将数据从一个组件传递到另一个组件并将该数据存储到 vue.js 中的数组中? - How to pass data from one component to another component and store that data into an array in vue.js? 如何将数据(书籍数组长度)从一个组件传递到 vue.js 中的另一个组件? - How to pass data(books array length) from one component to another component in vue.js? 在Vue js中将数据从父组件传递给子组件 - Pass data from parent component to child of child in Vue js 无法在Vue.JS中将数据从一个组件推送到另一个组件 - Unable to push data from one component to another in Vue.JS 从父组件动态传递 href 到子组件 vue.js - Pass href from parent component to child component dynamically vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM