【发布时间】:2015-11-13 21:35:39
【问题描述】:
我正在使用 Aurelia 框架。 每次用户导航到新路线/页面时,我都想在应用文件(app.ts/app.js)中获取 navigationInstruction 信息。
我尝试在应用程序的生命周期事件(激活和绑定)中获取此信息,但没有可用信息。
谁能帮我解决这个问题。
提前致谢。
【问题讨论】:
标签: javascript aurelia
我正在使用 Aurelia 框架。 每次用户导航到新路线/页面时,我都想在应用文件(app.ts/app.js)中获取 navigationInstruction 信息。
我尝试在应用程序的生命周期事件(激活和绑定)中获取此信息,但没有可用信息。
谁能帮我解决这个问题。
提前致谢。
【问题讨论】:
标签: javascript aurelia
订阅路由器的导航“成功”事件:
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();
}
}
【讨论】: