【问题标题】:Identify the current route in Aurelia确定 Aurelia 中的当前路线
【发布时间】:2015-11-13 21:35:39
【问题描述】:

我正在使用 Aurelia 框架。 每次用户导航到新路线/页面时,我都想在应用文件(app.ts/app.js)中获取 navigationInstruction 信息。

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

谁能帮我解决这个问题。

提前致谢。

【问题讨论】:

    标签: javascript aurelia


    【解决方案1】:

    订阅路由器的导航“成功”事件:

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

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 2020-03-16
      • 2018-01-01
      • 2016-04-25
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多