在Ember中获取当前路线名称 [英] Get current route name in Ember

查看:99
本文介绍了在Ember中获取当前路线名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的ember应用程序中获取当前的路由名称;我试过这个:
Ember App.Router.router.currentState未定义
但它不适用于我(有probablig的东西我是missimg ...)我使用Ember rc6,我有一个多语言的应用程序;在应用程序中我检测到浏览器的语言,并重定向到正确的页面:



this.transitionTo(userLang);



但是,只有当用户在主页上时才执行此操作,所以这样的一个例子:

  if(currentRoute =='home'){
this.transitionTo(userLang)
}
/ pre>

解决方案

您可以观察应用程序 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's currentPath 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 with App.get('currentPath');

E.g.

if (App.get('currentPath') == 'home'){
  this.transitionTo(userLang);
}

Hope it helps.

这篇关于在Ember中获取当前路线名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆