[英]How to get value from the Child component of another Parent in Vue js?
我有组件 A,它有 2 个子组件 C1 和 C2。 在 C1 组件中,有一个函数可以从与 C2 中相同的源获取数据。 对于 C1
<template> </template> <script> export default { url = 'abc.com'; name: 'c', data () { return { users:'', }}, methods: { getData: function () { $.get(url, function( data ) { this.users = data; var ab = this.users.ab ; var pq = this.users.pq ; )}; }} } </script>
对于 C2
<template> </template> <script> export default { url = 'abc.com'; name: 'c', data () { return { users:'', }}, methods: { getData: function () { $.get(url, function( data ) { this.users = data; var cd = this.users.cd ; var rs = this.users.rs ; )}; }} } </script>
而且我还有另一个组件 D
<template> </template> <script> export default { data () { return { }}, methods: { showDataABCD: function () { // here I want to access all data of users object of C1 and C2] // especially ab, cd }, showDataPQRS: function () { // here I want to access all data of users object of C1 and C2 //especially PQ, RS } } </script>
在这个 D 组件中,我需要访问这两个组件的对象。 我可以使用 state 或任何其他可能的方式吗?
有很多方法可以实现您的目标。
但是,如果您想从组件 D 触发搜索,您可能希望首先在 C1 和 C2 上添加自定义事件侦听器 (eventbus.$on...)。 (你也可以传递一个回调函数)
您可以调整我在此处描述的模式Vue.js edit variables in dynamic components
只需将要共享的对象作为来自父级的道具传递即可。 然后,当孩子们发出更新的数据时,让父级设置它,数据以“共享状态”向下流向其子级。
数据将流向父级 -> 子级 -> 子级更新 -> 父级接收更新 -> 所有子级更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.