我使用 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();
}
你能帮我一下吗?
最佳答案
您可能想尝试对组件使用activate 方法,而不是附加方法。例如:
export class HeaderBar {
private async attached(): Promise<void> {
return await this.loadNotifications();
}
private loadNotifications() {
// do your async stuff here...
console.log('yeej, it works!');
}
}
与原始代码片段相比的一些变化:
- 考虑到您没有使用参数和/或路由配置,使用 activate 是完全合理的,并且似乎满足您的需求(据我现在所知)
- 为了简洁起见,我删除了与您的问题无关的所有其他代码
- “异步”此方法没有问题,正如您在我的示例中看到的
Component Lifecycle 中也更详细地描述了 activate() 的用法。 Aurelia 文档的部分。
更新:对于 Aurelia 生命周期的差异,StackOverflow 问题 "Difference between a Component and a View in Aurelia (and their lifecycle)"可能也会感兴趣。
关于javascript - Aurelia 激活函数永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45076400/