Get current route name in Ember

前端 未结 10 2130
我在风中等你
我在风中等你 2020-11-30 05:20

I need to get the current route name in my ember application; I tried this: Ember App.Router.router.currentState undefined but it doesn\'t work for me (there is probablig so

10条回答
  •  猫巷女王i
    2020-11-30 05:51

    With the shift to components, it is harder to get route name. The best way is to add an initializer such as

    ember g initializer router
    

    (from command line), and

    export function initialize(application) {
      application.inject('route', 'router', 'router:main');
      application.inject('component', 'router', 'router:main');
    }
    
    export default {
      name: 'router',
      initialize
    };
    

    in a initializers/router.js. You can also inject into controller if you need to. Then just do simply

    this.get('router.currentRouteName');
    

    in JS, or

    {{router.currentRouteName}}
    

    in template.

    This is the only way I have found to get it reliably, and observable in Ember 2.4

提交回复
热议问题