gpt4 book ai didi

javascript - Aurelia 激活功能永远不会被调用

转载 作者:搜寻专家 更新时间:2023-10-30 21:23:33 24 4
gpt4 key购买 nike

我使用 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!');
}

}

与原始代码段相比的一些变化:

  • 考虑到您没有使用 params 和/或 routeconfig,使用 activate 是完全合理的并且似乎满足您的需求(据我现在所见)
  • 为了简洁起见,我删除了所有其他与您的问题无关的代码
  • 正如您在我的示例中所见,“异步”该方法没有问题

activate() 的用法在 Component Lifecycle 中也有更详尽的描述。 Aurelia 文档的一部分。

更新:对于 Aurelia 生命周期的差异,StackOverflow 问题 "Difference between a Component and a View in Aurelia (and their lifecycle)"可能也很有趣。

关于javascript - Aurelia 激活功能永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45076400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com