我使用Aurelia + WebStorm编写了一个工具栏。在这个工具栏中有一个激活功能,但它永远不会被自动调用。您可以在此处查看TypeScript代码:
import {autoinject} from "aurelia-dependency-injection";
import {RouteConfig, Router} from "aurelia-router";
import {bindable} from "aurelia-templating";
import {getLogger} from "aurelia-logging";
import {ActuatorApi, NotificationApi, SystemApi} from "gen/api";
@autoinject
export class HeaderBar {
private static LOG = getLogger("header-bar");
public notificationKey: string;
...
@bindable
public router: Router;
constructor(private actuatorApi: ActuatorApi, private notificationApi: NotificationApi,
private systemApi: SystemApi) {
this.isBatterieTestActive = true;
this.hrefForActuatoresList = "#/app/configuration/actuators/";
this.loadActuators();
}
public async activate(params: any, routeConfig: RouteConfig): Promise<void> {
return this.loadNotifications();
}
你能帮帮我吗?
答案 0 :(得分:4)
您可能希望尝试对组件使用激活方法,而不是附加。例如:
export class HeaderBar {
private async attached(): Promise<void> {
return await this.loadNotifications();
}
private loadNotifications() {
// do your async stuff here...
console.log('yeej, it works!');
}
}
与原始代码段相比有些变化:
在Aurelia文档的Component Lifecycle部分中也更详细地描述了activate()的用法。
更新:对于Aurelia生命周期的差异,StackOverflow问题"Difference between a Component and a View in Aurelia (and their lifecycle)"也可能会受到关注。