Can't pass params with redirect
DblK opened this issue · 10 comments
Inside a router function, I tried to redirect to a login page but I which to keep the params.
I thought that I can do that:
this.redirect('NameOrPAth', null, { queryParams: { a: 42 } })
But it seems not working. I also tried, hash
, params
, query
, randomThing
, etc..
Is there a way to do so? Or should I compute back all params inside the path
to make it work?
why not use Router.go? I'm not sure this.redirect still exists...
Edit :
I just tested it, it works
this.redirect('name', { eventId: 'blabla' }, { query: 'q=s' });
Weird, on my Blaze onCreated/onRendered function I could not get it at all (with Router.current().params.query
), either with go
or redirect
.
Whereas if I replace with a path with a manual ?q=s
then the params can be retrieved.
How did you retrieve the params? The same way as me?
Yes I retrieve the settings the same way as you.
Does your redirect work? your url ends up with /?q=s
... Iron router will probably no longer be updated, if you start you should use FlowRouter-extra ...
Yes I retrieve the settings the same way as you. Does your redirect work? your url ends up with /?q=s
Yes my redirect is working but no query params in it... unless I manually add them to the url to redirect to.
It's not a start of a project, so can't switch to another router without a lot of work. Thanks for your time to help me.
I still have the workaround with the params, so I might go that way but does not feel right...
can you share more code of your route?
router.ts
const route = (path: string, templateName: string, options?: routeOptionsType) => {
Router.route(path, function (this: RouteController) {
if (templateName === 'Home') {
if (!Meteor.loggingIn() && !Meteor.user()) {
// this.redirect(`/login`, null, {query: Router.current().params.query}); // Did not work...
this.redirect(`/login?${(new URLSearchParams(Router.current().params.query)).toString()}`); // This is working
return;
}
}
this.render(templateName);
}, { layoutTemplate: '', template: templateName, name: options?.routerTemplateName || templateName });
};
route('/dashboard', 'Home');
route('/login', 'Login');
login.ts
(with login.hbs.html
near to it)
import { Router } from 'meteor/iron:router';
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import './login.hbs.html';
Template.Login.onCreated(() => {
console.log('Login', Router.current().params.query);
});
and this ?
this.redirect(/login
, null, {query: new URLSearchParams(Router.current().params.query)).toString()});
This is not working either.
Okay we well work on it