识别 Aurelia 中的当前路线

Identify the current route in Aurelia

我正在使用 Aurelia 框架。 每次用户导航到新 route/page.

时,我都想在应用程序文件 (app.ts/app.js) 中获取 navigationInstruction 信息

我尝试在应用程序的生命周期事件(激活和绑定)中获取此信息,但没有可用信息。

谁能帮我解决这个问题。

提前致谢。

订阅路由器的导航"success"事件:

import {EventAggregator} from 'aurelia-event-aggregator';
import {inject} from 'aurelia-dependency-injection';

@inject(EventAggregator)
export class App {
  constructor(eventAggregator) {
    this.eventAggregator = eventAggregator;
  }

  navigationSuccess(event) {
    let instruction = event.instruction;    
    // todo: do something with instruction...
  }

  attached() {
    this.subscription = this.eventAggregator.subscribe(
      'router:navigation:success',
      this.navigationSuccess.bind(this));
  }

  detached() {
    this.subscription.dispose();
  }
}

这是一个使用 ES7 函数绑定和 ES6 解构的稍微不同的版本:

import {EventAggregator} from 'aurelia-event-aggregator';
import {inject} from 'aurelia-dependency-injection';

@inject(EventAggregator)
export class App {
  constructor(eventAggregator) {
    this.eventAggregator = eventAggregator;
  }

  navigationSuccess({ instruction }) {
    // todo: do something with instruction...
  }

  attached() {
    this.subscription = this.eventAggregator.subscribe(
      'router:navigation:success',
      ::this.navigationSuccess);
  }

  detached() {
    this.subscription.dispose();
  }
}