Passing query parameters using Router.go in iron router

Aashu Agarwal picture Aashu Agarwal · Aug 28, 2014 · Viewed 13.4k times · Source

I am trying to pass query parameters in Router.go like below:

var filter = 'abc';
var path = Router.current() && Router.current().path;
Router.go(path, {query: {filter: filter}});

But this does not change the url, it stills loads current path without query string only. But if I add the query parameter manually to path like:

Router.go(path+'?filter='+filter);

this works fine. But since I am trying to load same page with some filtered data. So clicking filter button repeatedly appends the filter string again and again to path.

What is the correct way of passing query string using iron router?

Answer

Max Hodges picture Max Hodges · Jun 6, 2015

Right there in the docs

Router.go('post.show', {_id: 1}, {query: 'q=s', hash: 'hashFrag'});

The above JavaScript will navigate to this url:

/post/1?q=s#hashFrag

https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#named-routes