在Ember中获取当前路线名称 [英] Get current route name in Ember
问题描述
我需要在我的ember应用程序中获取当前的路由名称;我试过这个:
Ember App.Router.router.currentState未定义
但它不适用于我(有probablig的东西我是missimg ...)我使用Ember rc6,我有一个多语言的应用程序;在应用程序中我检测到浏览器的语言,并重定向到正确的页面:
this.transitionTo(userLang);
但是,只有当用户在主页上时才执行此操作,所以这样的一个例子:
if(currentRoute =='home'){
/ pre>
this.transitionTo(userLang)
}
解决方案您可以观察
应用程序
的currentPath
,并在更改时将其设置为当前路由: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);
}
希望有帮助。
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 something i'm missimg...) I use Ember rc6 and I have a multilingual app; in the applicationRoute I detect the browser's language and I redirect to the correct page with:
this.transitionTo(userLang);
but I would like this to be executed only when user are on the home page, so something like this:
if (currentRoute == 'home'){ this.transitionTo(userLang) }
解决方案You could observe the
application
'scurrentPath
and set it to the current route accordingly when it changes:App = Ember.Application.create({ currentPath: '', }); App.ApplicationController = Ember.Controller.extend({ updateCurrentPath: function() { App.set('currentPath', this.get('currentPath')); }.observes('currentPath') }),
This way you have access to the
currentPath
when ever you want withApp.get('currentPath');
E.g.
if (App.get('currentPath') == 'home'){ this.transitionTo(userLang); }
Hope it helps.
这篇关于在Ember中获取当前路线名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!