this.transitionTo(userLang);
但我希望仅当用户在首页时才执行此操作,因此类似以下的代码:if (currentRoute == 'home'){
this.transitionTo(userLang)
}
this.transitionTo(userLang);
但我希望仅当用户在首页时才执行此操作,因此类似以下的代码:if (currentRoute == 'home'){
this.transitionTo(userLang)
}
注意:从Ember 3.16开始,原先的答案不仅建议使用,而且观察者也被强烈不建议使用。
要获取当前路由名称,您可以利用Router Service:https://api.emberjs.com/ember/3.18/classes/RouterService/properties/currentRouteName?anchor=currentRouteName
export default class MyComponent extends Component {
@service router;
get activeRoute() {
return this.router.currentRouteName;
}
}
App = Ember.Application.create({
currentPath: '',
});
App.ApplicationController = Ember.Controller.extend({
updateCurrentPath: function() {
App.set('currentPath', this.get('currentPath'));
}.observes('currentPath')
}),
这样一来,您可以通过 App.get('currentPath');
随时访问 currentPath
。
例如:
if (App.get('currentPath') == 'home'){
this.transitionTo(userLang);
}
这在1.3.0-beta上对我有用(快速查看1.1.2的源代码表明它也能够工作):
App.__container__.lookup('router:main').location.lastSetURL
请注意,文档说明:
目前,它依赖于浏览器中存在的hashchange事件。
然而,我认为强烈建议不要在生产代码中使用App.__container__
。更可接受的替代方案是使用App.Router.router.currentHandlerInfos
,它提供了有关当前Ember路由的信息。
另一个选项是在ApplicationController
上使用currentRouteName
。您可以将needs: ['application']
添加到控制器中,然后使用controllers.application.currentRouteName
访问路由名称。这将返回类似于posts.index
的内容。
App.Router.router.currentHandlerInfos
不仅包含有关当前Ember路由及其上下文(即:模型)的信息,还包括所有父级路由的类似信息。如果需要这些数据,那么这确实是一种可行的方法! - nickiaconisApp.__container__.lookup()
非常有用。 - datchung随着向组件的转变,获取路由名称变得更加困难。最佳方法是添加一个初始化器,例如
ember g initializer router
(从命令行开始),并且
export function initialize(application) {
application.inject('route', 'router', 'router:main');
application.inject('component', 'router', 'router:main');
}
export default {
name: 'router',
initialize
};
在initializers/router.js中,您也可以将其注入到控制器中(如果需要)。然后只需简单地执行
this.get('router.currentRouteName');
在JS中,或者{{router.currentRouteName}}
在模板中。
这是我发现的唯一可靠且在Ember 2.4中可观察到的方法。
EmberRouter
甚至不是一个服务,只是在这里被用作一个。我强烈建议使用在Ember 2.15中添加的RouterService
。 - jelhan如果您想在组件或控制器中获取当前路由,您可以注入路由服务(routing: Ember.inject.service('-routing')
)
(有关更多信息),然后使用:
this.get('routing.currentRouteName')
或 this.get('routing.currentPath')
使用组件和计算属性的示例:
import Ember from 'ember';
export default Ember.Component.extend({
routing: Ember.inject.service('-routing'),
checkMyRouteName: Ember.computed('routing.currentRouteName', function() {
return this.get('routing.currentRouteName');
})
})
使用控制器(Controller)和计算属性(Computed Property)的示例:
import Ember from 'ember';
export default Ember.Controller.extend({
routing: Ember.inject.service('-routing'),
checkMyRouteName: Ember.computed('routing.currentRouteName', function() {
return this.get('routing.currentRouteName');
})
})
在您的路由中,您只需要使用this.routeName
。
带有路由的示例:
import Ember from 'ember';
export default Ember.Route.extend({
checkMyRouteName() {
return this.routeName;
}
})
-routing
服务。在 Ember 2.15 中已经添加了公共的 RouterService
,并且对于 Ember >= 2.4 进行了 polyfill。 - jelhan更新一下,在Ember 1.8.1中,我们可以通过 this.routeName
在Ember.Route对象内获取路由名称。
this.routeName
返回“application”,并不是很有用...如果能像route.sub.sub这样就好了。 - Jan WerkhovenEmber的命名空间API现在有一个getOwner
方法,非常适用于查找currentRouteName
或其他路由属性。
const owner = Ember.getOwner(this);
const currentRoute = owner.lookup('router:main').currentRouteName;
const routeInfo = owner.lookup(`route:${currentRoute}`).get('info');
// etc.
我创建了一个Ember Twiddle示例来进行演示。使用“输出”面板上方的文本输入框,可以访问其他路由,例如/blue
、/green
或/red
。
EmberRouter
的currentRouteName
属性不仅是私有的,甚至未记录在文档中。您应该使用公共的RouterService
,它在Ember 2.15中添加,并且对于Ember >= 2.4进行了填充。 - jelhan截至目前,Ember 1.7.0版本中,您可以通过调用this.routeName
在当前路由中获取当前路由。
RouterService
。它提供了当前路由的名称,即currentRouteName
属性。如果您仍在使用旧版本(2.4-2.14),则存在polyfill。import Component from '@ember/component';
export default Component.extend({
router: service(),
isHomeRoute: computed('router.currentRouteName', function() {
return this.router.currentRouteName === 'home';
}),
});
RouterService
至少在当前版本(写作时为3.12
)上是有效的。"home"
不是/
。根URL称为"index"
。我曾经有同样的问题。后来我开始探索路由器。它总是有一个状态对象,可以从任何路由中获取。
var route = this;
var handlerInfos = route.get("router.router.state.handlerInfos");
var currRouteHandlerInfo = handlerInfos[handlerInfos.length-1];
var currRouteName = currRouteHandlerInfo.name; //"home"
就是这样。现在你有了当前路由的名称!
如果你想要当前路由的参数,
var routerParams = this.get("router.router.state.params");
var currRouteParams = routerParams[currRouteName]; //{ homeId : "1" }
this.controller.target.currentPath