当父级与Aurelia中的组件类型相同时传递父级 [英] Passing the parent when parent is the same component type in Aurelia

查看:63
本文介绍了当父级与Aurelia中的组件类型相同时传递父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题以前的标题为当父项在Aurelia中是相同类型时通过DI获取父项",但是由于我的元素是如何嵌套的,所以仅绑定父项更有意义元素,因此标题已更改以反映这一点.

This question used to be titled "Getting parent via DI when parent is the same type in Aurelia" but due to how my elements are nested, it makes more sense to just bind the parent to the element, so the title has been changed to reflect this.

如果我有一个自定义元素Thing,它有一个子元素Thing(有另一个子元素Thing,依此类推),那么当类相同时,如何注入父实例?

If I have a custom element, Thing which has a child Thing (which has a another child Thing, etc), how can I inject the parent instance when the class is the same?

export class Thing {
    static inject = [Thing]; // <-- no reference to Thing as we are inside the class
    constructor(parentThing) {
        this.parent = parentThing;
    }
}

进一步的复杂性是,根Thing元素将没有父元素,因此DI需要允许可选注入.

As a further complexity, the root Thing element will have no parent, so the DI needs to allow for optional injection.

推荐答案

使用DI看起来不正确或没有这个问题.如果某个元素需要从其使用者接收某些特定输入数据,则@bindable是我自然的第一个想法.那么如何在Thing中创建@bindable parentThing?

This problem doesn't look right or necessary to use DI. If an element need to receive some specific input data from its consumer, @bindable would be my natural first thinking. So how about creating a @bindable parentThing in Thing?

另一方面,如果要访问父绑定上下文,请考虑bind()

In other hand, if what you want is to access parent binding context, consider bind() component life cycle.

这篇关于当父级与Aurelia中的组件类型相同时传递父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆