没有理由不这样做:)
import {Router, RouterConfiguration} from 'aurelia-router';
import { EventAggregator } from 'aurelia-event-aggregator';
import {autoinject} from 'aurelia-framework';
@autoinject
export class App {
router: Router;
ea: EventAggregator;
navEvent: any;
constructor(eventAggregator: EventAggregator) {
this.ea = eventAggregator;
}
configureRouter(config: RouterConfiguration, router: Router) {
config.title = 'Intterra Management';
config.map([
{ route: ['', 'home'], name: 'home', moduleId: 'home', nav: false, title: '' },
{ route: 'logs', name: 'logs', moduleId: 'logs', nav: true, title: 'Logs' }
]);
this.router = router;
}
attached() {
this.navEvent = this.ea.subscribe('router:navigation:complete', response => {
localStorage['state'] = location.hash;
});
}
detached() {
this.navEvent.dispose();
}
...
我确认当我导航时事件正在触发,但订阅的功能未被调用。有什么想法吗?
答案 0 :(得分:2)
由于在所有路由之后调用附加函数,因此您最有可能在调用事件后订阅该事件。你应该把它放在activate
函数中。
activate() {
this.navEvent = this.ea.subscribe('router:navigation:complete', response => {
localStorage['state'] = location.hash;
});
}
deactivate() {
this.navEvent.dispose();
}
答案 1 :(得分:0)
不确定您是否找到了问题的解决方案,但请直接尝试使用this.ea
。
this.ea.subscribe("router:navigation:complete", response => {
localStorage['state'] = location.hash;
});
不确定在构造函数中订阅所述事件是否有帮助?