当父级与Aurelia中的组件类型相同时传递父级

编程入门 行业动态 更新时间:2024-10-25 13:15:40
本文介绍了当父级与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中的组件类型相同时传递父级

本文发布于:2023-10-31 06:36:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   类型   Aurelia

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!