最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Identify the current route in Aurelia - Stack Overflow

programmeradmin4浏览0评论

I am using Aurelia framework. I want to fetch navigationInstruction info in app file(app.ts/app.js) every time user navigate to new route/page.

I have tried to fetch this information in the life-cycle events(activate and bind) of app, but no information is available.

Can anybody help me to solve this issue.

Thanks in advance.

I am using Aurelia framework. I want to fetch navigationInstruction info in app file(app.ts/app.js) every time user navigate to new route/page.

I have tried to fetch this information in the life-cycle events(activate and bind) of app, but no information is available.

Can anybody help me to solve this issue.

Thanks in advance.

Share Improve this question asked Nov 13, 2015 at 9:30 AnkurAnkur 7408 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

subscribe to the router's navigation "success" event:

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

Here's a slightly different version using ES7 function binding and ES6 destructuring:

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