Aurelia $parent在第一次页面加载时未定义



在Aurelia中,我目前无法绑定到登录页面中父(app.ts(的字段。 如果我显式使用$parent,我会得到一个异常,告诉我$parent没有定义。

如果我路由到另一个页面并返回到现在定义的登录页面$parent并且我可以直接访问父级的字段,而无需使用$parent关键字。

我敢肯定它更早,但我认为一些更新的软件包破坏了它。(更新后没有识别(

<h1>${testString}</h1><!-- Nothing displayed -->
<h1>${$parent.testString}</h1><!-- ERROR [app-router] TypeError: Cannot read property 'testString' of undefined -->

两者都在页面加载后最初不起作用,但在路由回此页面后不起作用。

我错过了什么?

使用 aurelia-binding 2.1.5、aurelia-framework 1.3.0、aurelia-router 1.6.3

不要使用$parent,只需使用依赖注入来访问父级的视图模型。

下面是一个 GistRun 示例。

测试孩子.html

<template>
<h1>${app.message}</h1>
</template>

测试孩子.js

import {inject} from 'aurelia-framework';
import {App} from './app';
@inject(App)
export class TestChild {
constructor(app) {
this.app = app;
}
}

应用.html

<template>
<require from="./test-child"></require>
<test-child></test-child>
</template>

应用.js

export class App {
message = 'Hello World';
}

最新更新