Passing parameters between routes in Ember 2.x

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:01:35

问题


While going from one route to another, I want to pass some data (especcially arrays). How is it possible?

Why can't we use query-params with arrays?

Is it a problem storing data in a specific service during transition?

Note: I know there are some old questions those are nearly the same with this question. But their selected answers are no more applicable for Ember 2.x. Those questions are: 1, 2.


回答1:


I´m not sure if queryparams won´t work with arrays as I only used it with single ids, but it would not be a good solutions even if it worked, there´s a limit on how much you can send by parameters and you should not bother any user with your data.

  1. Just create a model to save your data for local use, so you can simply use the ember store

  2. Use a service you´ll have to inject in every controller you want to use your data

I would prefer the model/store variant so you´ll be able to observe and just follow the normal flow which is also good if someone else has to maintain your code.




回答2:


UPDATED

After testing with "transition.data"; not updating the history seems as a problem for us. So we again use "queryParams". The constraint is: do not pass a complex object between routes

OLD ANSWER

I'm using transition object for this purpose in an action while routing as the following:

let transition = router.transitionTo(route, model);
transition.data[propName] = propValue;

Also I wrote a component to use this code as link-to.



来源:https://stackoverflow.com/questions/39286750/passing-parameters-between-routes-in-ember-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!