我正在使用Aurelia框架。 我想在每次用户导航到新路线/页面时在app文件(app.ts / app.js)中获取 navigationInstruction 信息。
我试图在应用的生命周期事件(激活和绑定)中获取此信息,但没有可用的信息。
任何人都可以帮我解决这个问题。
提前致谢。
答案 0 :(得分:10)
订阅路由器的导航“成功”事件:
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();
}
}