在ember 2.0中获取currentPath

问题描述 投票:1回答:3

在Ember 2.0中,控制器将被弃用。如何访问currentPath。目前我正在做以下事情。

  needs: ["application"],

  routeName : function() {
    return this.get('controllers.application.currentPath');
  }.property('controllers.application.currentPath),
ember.js
3个回答
4
投票

你可以做点什么

applicationController: Ember.inject.controller("application"),
routeName : function() {
    return this.get('applicationController.currentPath');
}.property('applicationController.currentPath)

2
投票

如果我没记错,ember 1.8添加了routeName属性来路由对象,你可以通过调用this.routeName从路径访问它(我假设这是你想要的,因为你的cp被命名为)。

另外,您应该开始使用新的计算属性语法:

routeName: Ember.computed('controllers.application.currentPath', function() {
  return this.get('controllers.application.currentPath');
}),

1
投票

以3.9 API文档为例,以下是您现在的工作方式。可能建议使用公共API(currentRouteName,currentURL),但现在就像这样非常容易重构。

export default Service.extend({
  router: service(),

  whereAmI: computed('router.currentRouteName', function() {
    return this.router._router.currentPath; // 'users.index'
  })
});

https://api.emberjs.com/ember/3.9/classes/RouterService https://github.com/emberjs/ember.js/blob/v3.9.0/packages/%40ember/-internals/routing/lib/system/router.ts#L1631

© www.soinside.com 2019 - 2024. All rights reserved.