Get current route name in Ember

前端 未结 10 2116
我在风中等你
我在风中等你 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 05:36

    This worked for me on 1.3.0-beta (and a quick glance at the source for 1.1.2 suggests it would work there too):

    App.__container__.lookup('router:main').location.lastSetURL
    

    Note that the documentation states:

    At present, it relies on a hashchange event existing in the browser.

    However, I believe it's strongly suggested that App.__container__ not be used in production code. A more acceptable alternative would be to use App.Router.router.currentHandlerInfos, which provides information on the current Ember route.

    Yet another option is currentRouteName on the ApplicationController. You can add needs: ['application'] to your controller, then access the route name with controllers.application.currentRouteName. This will return something like posts.index.

提交回复
热议问题