这是这个问题的后续:访问 Aurelia 中的 DOM 元素 https://stackoverflow.com/questions/29863044/access-a-dom-element-in-aurelia
屏幕激活生命周期中是否有一个钩子允许我运行代码after ref
绑定已设置?目前看来还有一段时间activate
当ref
绑定尚未设置,然后在某个时刻它们会被激活。我通过添加一个来测试这个<div ref="myDiv"></div>
到接近底部的welcome.html
在最新(v0.13.0)骨架导航存储库的克隆版本中,并测试视图模型中引用的存在,如下所示:
export class Welcome{
heading = 'Welcome to the Aurelia Navigation App!';
firstName = 'John';
lastName = 'Doe';
testMyDiv() {
console.log("Getting my div")
console.log(this.myDiv)
}
get fullName(){
this.testMyDiv()
return `${this.firstName} ${this.lastName}`;
}
welcome(){
alert(`Welcome, ${this.fullName}!`);
}
}
模板底部的片段...
<button type="submit" class="btn btn-default">Submit</button>
</form>
<div ref="myDiv"></div>
</section>
</template>
这是我在控制台中看到的快照......
welcome.js:10 Getting my div
welcome.js:11 undefined
welcome.js:10 Getting my div
welcome.js:11 undefined
welcome.js:10 Getting my div
welcome.js:11 <div ref="myDiv" class="au-target"></div>
welcome.js:10 Getting my div
welcome.js:11 <div ref="myDiv" class="au-target"></div>
(continues)
像这样的打印输出会无限期地持续下去。你可以看到fullName()
如果名称更改,则定期调用以更新屏幕(我认为这是脏检查)...但是您可以看到,在开始时有一个时期,当引用div
作为视图模型的属性无效,然后它是有效的。有人可以解释一下吗?有没有办法在之后挂接到视图模型ref
变得有效?
一般来说,绑定在以下时间后被处理并可用bind
打回来。但是,在这种情况下,由于您需要访问 DOM 元素,因此需要绑定 ViewModeland附加到视图,因此使用attached
打回来。
class ViewModel {
bind() {
this.refItem == undefined; // true
}
attached() {
this.refItem == undefined; // false
}
}
正如您在评论中指出的,有关激活器回调的更多信息可在此处找到:http://aurelia.io/docs.html#extending-html http://aurelia.io/docs.html#extending-html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)