[英]How to add props from parent component to child component of child component
I have to pass props from parent component to child component of the child component.我必须将道具从父组件传递给子组件的子组件。 Please help since I'm new to Vue.js请帮忙,因为我是 Vue.js 的新手
parent component --> child component --> child component
Im calling rolodex from member directory component from different components both follow page and connections page but I need to know from where rolodex component it has been called?我从不同组件的成员目录组件中调用 rolodex,包括跟随页面和连接页面,但我需要知道它是从哪里调用的 rolodex 组件? so that I can display in rolodex component like "rolodex called from < >" I am calling <Rolodex>
from multiple components.这样我就可以在 rolodex 组件中显示,例如“从 < > 调用的 rolodex”我从多个组件调用<Rolodex>
。
<Follow Component>---><member directory>---><rolodex>,<search-results>
<Connections component>--><member directory>---><rolodex>,<search-results>
Use an EventBus, create a js file in your root directory where your main.js file is, I would call it event-bus.js and just add this:使用 EventBus,在 main.js 文件所在的根目录中创建一个 js 文件,我将其命名为 event-bus.js 并添加以下内容:
import Vue from 'vue';
const EventBus = new Vue();
export default EventBus;
Then import your EventBus in any components you need:然后在您需要的任何组件中导入您的 EventBus:
import EventBus from '@/event-bus'
Send it like this:像这样发送:
let data = 'data'
EventBus.$emit('test', data)
Receive it like this:像这样接收它:
EventBus.$on('test', (data) => {
console.log(data)
})
This explains it more in depth: https://medium.com/easyread/vue-as-event-bus-life-is-happier-7a04fe5231e1这更深入地解释了它: https ://medium.com/easyread/vue-as-event-bus-life-is-happier-7a04fe5231e1
You've got a few options.你有几个选择。
You could just pass the props down, one component at a time.你可以只传递道具,一次一个组件。 Each component passes down the relevant values to the children that need them.每个组件将相关值传递给需要它们的子级。 This should be your default option but there are some problems with it:这应该是您的默认选项,但它存在一些问题:
An alternative is provide
/ inject
:另一种方法是provide
/ inject
:
https://v2.vuejs.org/v2/guide/components-edge-cases.html#Dependency-Injection https://v2.vuejs.org/v2/guide/components-edge-cases.html#Dependency-Injection
https://v2.vuejs.org/v2/api/#provide-inject https://v2.vuejs.org/v2/api/#provide-inject
This is generally considered an advanced feature and there are several caveats around using it.这通常被认为是一项高级功能,使用它有几个注意事项。 However, it does allow a component to pass 'long-range props' down to a descendant, skipping over any components in between without them knowing anything about it.但是,它确实允许组件将“远程道具”向下传递给后代,跳过中间的任何组件,而他们对此一无所知。
If data changes need passing back up to the source it can get tricky as provide
/ inject
only really works in one direction.如果数据更改需要传回源,它可能会变得很棘手,因为provide
/ inject
仅在一个方向上真正起作用。 If data is passed as a reference type it can be modified in the descendant directly, though that would be considered a violation of one-way data flow.如果数据作为引用类型传递,则可以直接在后代中对其进行修改,尽管这将被视为违反单向数据流。 In theory is would be possible to pass a callback function via provide
/ inject
that could be used in place of an event.从理论上讲,可以通过provide
/ inject
传递一个回调函数来代替事件。 It all gets a bit messy.一切都变得有些混乱。
When data needs to jump around the component tree the standard solution is the Vuex store:当数据需要在组件树中跳转时,标准的解决方案是 Vuex 存储:
https://vuex.vuejs.org/ https://vuex.vuejs.org/
I won't go into a lot of detail about Vuex, there's plenty been written elsewhere about it, but the key idea is that data is stored in a singleton, accessible from anywhere in your application.我不会详细介绍 Vuex,其他地方已经写了很多关于它的文章,但关键思想是数据存储在单例中,可以从应用程序的任何地方访问。
In the context of this question the problem would likely be context.在这个问题的背景下,问题可能是背景。 If you have multiple instances of rolodex
shown at the same time they would both be looking at the same state in the store.如果您同时显示多个rolodex
实例,它们将在商店中查看相同的状态。 It would only really work well for a single instance at once, with the ancestor setting the state and the descendant reading it.它一次只对一个实例真正有效,祖先设置状态,后代读取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.