从控制器或组件访问当前路由名称

jax*_*jax 8 ember.js

我需要根据当前路由名称将"活动"类应用于引导选项卡.路由对象包含"routeName"但是如何从控制器或组件访问它?

ble*_*enm 9

用这个 this.controllerFor('application').get('currentRouteName');

  • 这是否适用于组件? (4认同)
  • 这在Ember 2中不起作用.至少不是来自`controller`,`this.controllerFor`不再被定义.我认为你可以做到,但你需要使用`registry`或`container`或类似的东西. (2认同)

Jef*_*y C 5

实际上,您不需要自己申请活动课程.一个link-to助手将做给你.

看到这里:

{{link-to}}当应用程序的当前路由与提供的routeName匹配时,将应用CSS类名"active".例如,如果应用程序的当前路线是"photoGallery.recent",则以下使用{{link-to}}:

{{#link-to 'photoGallery.recent'}}
  Great Hamster Photos
{{/link-to}}
Run Code Online (Sandbox Code Playgroud)

会导致

<a href="/hamster-photos/this-week" class="active">
  Great Hamster Photos
</a>
Run Code Online (Sandbox Code Playgroud)


Dio*_*ves 3

对于 Ember 2,您可以从控制器尝试:

appController: Ember.inject.controller('application'),
currentRouteName: Ember.computed.reads('appController.currentRouteName')
Run Code Online (Sandbox Code Playgroud)

然后你可以将它传递给组件。