2017-08-26 55 views
0

我有我的自定义Ember组件在控制器内。访问组件内的当前Ember路由

现在我试图让当前路由名称动态

所以我可以访问使用

this.get('parentView') 

控制器不过,我不能在此之后获得的电流路径名。我如何获得当前的路由名称? 我试着做

this.get('parentView').routeName 

回答

1

首先,你应该根据一个部件的电流路径没有实现逻辑。您的组件不应该知道它们被放置的路线。但当然也有可能某些特殊情况下,如breadcrump组件,菜单组件...

不管怎样,你的问题的答案是:

  1. 通过使用application controller

application controller有一个currentRouteName属性。

可以使用application controller如:

Ember.getOwner(this).lookup('controller:application').get('currentRouteName') 
  • 通过使用router
  • 要么注入private-router service到您的组件或查找为路由器实例如:

    Ember.getOwner(this).lookup('router:main').get('currentRouteName'); 
    
    +1

    您不能注入控制器实例以外的其他控制器。 – kumkanillam

    1

    在余烬2.15将成为此路由器服务。目前有is a polypill for older ember versions

    编辑:Ember 2.15已于2017年9月1日发布。您可以在release notesapi documentation中找到有关RouterService的一些信息。请注意,api文档目前没有提及由于代码中缺少文档而暴露的属性。除了ember-router-service-polyfill,您还可以在早期版本中通过-routing访问专用RoutingService

    相关问题