Aurelia $parent 在第一页加载时未定义
Aurelia $parent is undefined on first page load
在 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 example。
测试-child.html
<template>
<h1>${app.message}</h1>
</template>
测试-child.js
import {inject} from 'aurelia-framework';
import {App} from './app';
@inject(App)
export class TestChild {
constructor(app) {
this.app = app;
}
}
app.html
<template>
<require from="./test-child"></require>
<test-child></test-child>
</template>
app.js
export class App {
message = 'Hello World';
}
在 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 example。
测试-child.html
<template>
<h1>${app.message}</h1>
</template>
测试-child.js
import {inject} from 'aurelia-framework';
import {App} from './app';
@inject(App)
export class TestChild {
constructor(app) {
this.app = app;
}
}
app.html
<template>
<require from="./test-child"></require>
<test-child></test-child>
</template>
app.js
export class App {
message = 'Hello World';
}