将对象传递到下一个同级组件 [英] Passing objects to next sibling components
问题描述
我在AureliaJS组件中的结构是:
My structure in AureliaJS of components is:
<parent>
<child1>
<child2>
</parent>
我在child1
中有一个对象,该对象是通过ajax请求获得的:
I have an object in child1
which I get with ajax requests:
export class Child1 {
fechedObject = {}:
}
我需要具有双向绑定且在第二个组件中可观察到的此属性
I need this property with two-way binding and observable in the second component
export class Child2 {
// I need this fechedObject here
}
获得它的最佳方法是什么?
What is the best approach to get it?
推荐答案
我认为,最好的方法是在两个子模型上都使用双向绑定,以通过父代中的双向绑定来绑定模型.
I believe the best approach here is using two-way binding on both child models, to bind the model via two-way binding in the parent.
在您的parent.html
中,您将需要以下内容:
In your parent.html
, you would need this:
<child1 fetched-object.two-way="fetchedObject"></child1>
<child2 fetched-object.two-way="fetchedObject"></child2>
在两个子视图模型中,您都将变量声明为bindable
:
And in both child view-models, you'd declare the variable as a bindable
:
bindable()
public fechedObject;
这样,在两个孩子中发生的所有编辑都将传递给另一个孩子.如果要防止child2
中的编辑影响child1
中的对象,则可以简单地在child2
上使用fechedObject.one-way
或fechedObject.bind
进行单向绑定.
This way any edits that happen in either children, will get passed on to the other child. If you want to prevent edits in child2
from affecting the object in child1
, you can simply bind one-way using fechedObject.one-way
or fechedObject.bind
on your child2
.
这篇关于将对象传递到下一个同级组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!