这是如何做到这一点:https ://gist.run?id=b075366d29f2400d3cc931f6fc26db24
应用程序.html
<template>
<require from="./thing"></require>
<thing name="A">
<thing name="B">
<thing name="C">
<thing name="D">
</thing>
</thing>
</thing>
</thing>
</template>
应用程序.js
export class App {
}
可选的parent.js
import {resolver} from 'aurelia-dependency-injection';
@resolver()
export class OptionalParent {
constructor(key) {
this.key = key;
}
get(container) {
if (container.parent && container.parent.hasResolver(this.key, false)) {
return container.parent.get(this.key)
}
return null;
}
static of(key) {
return new OptionalParent(key);
}
}
东西.html
<template>
<h3>
My Name is "${name}".
<span if.bind="parent">My parent's name is "${parent.name}".</span>
<span if.bind="!parent">I don't have a parent.</span>
</h3>
<content></content>
</template>
东西.js
import {bindable, inject} from 'aurelia-framework';
import {OptionalParent} from './optional-parent';
@inject(OptionalParent.of(Thing))
export class Thing {
@bindable name;
constructor(parent) {
this.parent = parent;
}
}