Search code examples
javascripttypescriptaurelia

Aurelia activate function never be called


I programmed a tool-bar using Aurelia + WebStorm. In this tool-bar there is an activate function but it never be called automatically. You can see the TypeScript code here:

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();
  }

Could you please help me?


Solution

  • You might want to try using the activate method for a component, instead of attached. For example:

    export class HeaderBar {
    
        private async attached(): Promise<void> {
            return await this.loadNotifications();
        }
    
        private loadNotifications() {
            // do your async stuff here...
            console.log('yeej, it works!');
        }
    
    }
    

    Some changes in comparison to your original snippet:

    • Considering you're not using the params and/or routeconfig, using activate is perfectly plausible and seems to suit your needs (as far as I can see now)
    • For the sake of brevity I've removed all other non-relevant code to your question
    • It's no problem to 'async' this method, as you can see in my example

    The usage of activate() is also described more thoroughly in the Component Lifecycle section of the Aurelia docs.

    Update: For the difference in Aurelia lifecycles, the StackOverflow question "Difference between a Component and a View in Aurelia (and their lifecycle)" may be of interest as well.