中的路由访问查询参数

中的路由访问查询参数

本文介绍了如何从 Ember 1.7 中的路由访问查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 1.7 Ember 应该支持查询参数.我在控制器中使用它们没有问题,但我想在 Route 中访问它们,理想情况下在 beforeModel 钩子中但模型钩子也可以工作.

In 1.7 Ember should support Query Parameters. I have no problems using them in controller but I'd like to access them in Route, ideally in beforeModel hook but model hook would work as well.

API 文档 提到了 queryParam beforeModel 钩子的参数,但如果我尝试使用它,它总是未定义的.

The API docs mention a queryParam parameter for the beforeModel hook but if I try to use it, it is always undefined.

查询参数指南 似乎建议查询参数应该是可访问的作为模型钩子的第一个参数的一部分.但这也是未定义的.有关示例,请参阅下面的代码.

The Query Parameters guide seems to suggest that the query parameters should be accessible as a part of the first parameter to the model hook. But that is also undefined. See the code below for examples.

有没有办法从 Route 访问查询参数?

Is there a way to access the query parameters from Route?

感谢您的帮助.

App.ApplicationRoute = Em.Route.extend({
   beforeModel: function(transition, queryParams){
       console.log(queryParams.test); //undefined at /?test=123
   },
   model: function(params){
       console.log(params.test); //undefined at /?test=123
   }
});

推荐答案

很确定这是一个错误,但您可以同时通过转换对象访问它们:

Pretty sure it's a bug, but you can access them in the meantime via the transition object:

App.ApplicationRoute = Em.Route.extend({
   beforeModel: function(transition){
       console.log(transition.queryParams.test);
   }
}

这篇关于如何从 Ember 1.7 中的路由访问查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:19