在Aurelia中,当使用TypeScript编写视图模型时,可以使用@computedFrom
装饰器来确保视图在激活时更新。
以下是一个示例:
import { computedFrom } from 'aurelia-framework';
export class MyViewModel {
public currentView: string = 'Initial View';
public activate() {
setTimeout(() => {
this.currentView = 'Updated View';
}, 2000);
}
@computedFrom('currentView')
public get view() {
return this.currentView;
}
}
在上面的示例中,我们首先导入computedFrom
装饰器。然后,我们定义了一个名为currentView
的属性,并将其初始化为'Initial View'
。
在activate
方法中,我们使用setTimeout
模拟一个异步操作,在2秒后更新currentView
属性的值为'Updated View'
。
然后,我们使用@computedFrom
装饰器来定义一个名为view
的计算属性,并指定它依赖于currentView
属性。
这样,当currentView
属性的值发生变化时,view
计算属性就会自动更新,从而更新当前视图。
请注意,要使@computedFrom
装饰器正常工作,需要确保使用了Aurelia的绑定系统,即在视图中使用${view}
表达式来绑定到view
属性。
希望这可以帮助到你!