我需要在我的ember应用程序中获取当前路由名称; 我试过这个: Ember App.Router.router.currentState未定义 但它对我不起作用(有可能是我想念的东西......)我使用Ember rc6并且我有一个多语言应用程序; 在applicationRoute我检测到浏览器的语言,我重定向到正确的页面:
this.transitionTo(userLang);
但我希望只有当用户在主页上时才执行此操作,所以这样的事情:
if (currentRoute == 'home'){
this.transitionTo(userLang)
}
Run Code Online (Sandbox Code Playgroud)
Lin*_*nda 25
这对我来说在1.3.0-beta上工作(快速浏览1.1.2的来源表明它也可以在那里工作):
App.__container__.lookup('router:main').location.lastSetURL
Run Code Online (Sandbox Code Playgroud)
请注意,文档说明:
App.__container__.lookup('router:main').location.lastSetURL
Run Code Online (Sandbox Code Playgroud)
但是,我认为强烈建议App.__container__不要在生产代码中使用.更可接受的替代方案是使用App.Router.router.currentHandlerInfos,它提供有关当前Ember路线的信息.
另一个选择是currentRouteName在ApplicationController.您可以添加needs: ['application']到控制器,然后使用访问路径名称controllers.application.currentRouteName.这将返回类似的东西posts.index.
Xer*_*dea 18
随着向组件的转变,获取路由名称变得更加困难.最好的方法是添加初始化程序,如
ember g initializer router
Run Code Online (Sandbox Code Playgroud)
(来自命令行),和
export function initialize(application) {
application.inject('route', 'router', 'router:main');
application.inject('component', 'router', 'router:main');
}
export default {
name: 'router',
initialize
};
Run Code Online (Sandbox Code Playgroud)
在initializers/router.js中.如果需要,您也可以注入控制器.那就干脆做吧
this.get('router.currentRouteName');
Run Code Online (Sandbox Code Playgroud)
在JS,或
{{router.currentRouteName}}
Run Code Online (Sandbox Code Playgroud)
在模板中.
这是我发现可靠的唯一方法,并且可以在Ember 2.4中观察到
int*_*xel 17
你可以观察到application的currentPath,并相应地设置为当前的路由时,它的变化:
App = Ember.Application.create({
currentPath: '',
});
App.ApplicationController = Ember.Controller.extend({
updateCurrentPath: function() {
App.set('currentPath', this.get('currentPath'));
}.observes('currentPath')
}),
Run Code Online (Sandbox Code Playgroud)
这样,您就可以访问currentPath时,曾经要与App.get('currentPath');
例如
if (App.get('currentPath') == 'home'){
this.transitionTo(userLang);
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
Hub*_*der 16
如果要在组件或控制器中获取当前路由,可以注入路由服务(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');
})
})
Run Code Online (Sandbox Code Playgroud)
控制器和计算属性的示例:
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');
})
})
Run Code Online (Sandbox Code Playgroud)
您需要的路线中的当前路线 this.routeName
路线示例:
import Ember from 'ember';
export default Ember.Route.extend({
checkMyRouteName() {
return this.routeName;
}
})
Run Code Online (Sandbox Code Playgroud)
Gja*_*don 13
就像更新一样,在Ember 1.8.1中,我们可以通过执行来获取Ember.Route对象中的routeName this.routeName.
| 归档时间: |
|
| 查看次数: |
37659 次 |
| 最近记录: |