Meteor Router.go() doesn't rederect, but it works in Chrome's console
Asked Answered
P

4

6

Meteor Router.go() doesn't work. It just flash a new url for few milliseconds in the browser, and the page didn't switch.

Sorry I can't find any clue how this wired thing happen..!

Template.Post.events({ 
'click a': function() { 
    Router.go('mainPage');  
});

Router.route('/', {
  name: 'mainPage',
  template: 'mainPage'
});

Update: I input Router.go('mainPage'); in Chrome console. It works and return undefined.

Pronucleus answered 25/5, 2015 at 20:22 Comment(0)
P
13

To avoid this miserable, horrible experience for everyone, let me post my solution and answer myself:

When Router.go() redirects the URL, the URL also instantly redirects to href="#" or href="". Thus, it disables the redirection from Router.go().

The way to solve it is just to NOT put href="" in the <a> tag. Also, you can add this css:

a:hover {
    cursor: pointer;
}

to show that the tag is actually clickable.

Pronucleus answered 27/7, 2015 at 0:12 Comment(0)
C
5

You can avoid having to remove the href by calling event.preventDefault() which stops the execution of any additional bubbled events like the href click:

"click #aLinkId": function(event, template) {
    event.preventDefault();
    Router.go('/newLocation');
}
Cowes answered 19/5, 2016 at 8:32 Comment(0)
O
4

I had this problem too, wrapping it in Meteor.setTimeout was the only way to make it work.

'click a': function() { 
    Meteor.setTimeout(function(){ Router.go('mainPage'); }, 10); 
}
Osmose answered 26/5, 2015 at 7:32 Comment(3)
that's amazing...but have you found out why this wired thing happen?Pronucleus
It seems there is a problem on <a> when it has href attribute. You need to specify e.preventDefault(), look here github.com/iron-meteor/iron-router/issues/1038Osmose
Oh GREAT!!! That's it!! Once remove the href in <a> Router.go works!! It is a horrible bug!Pronucleus
P
0

Can we see your Router definitions?

At least setup one route:

Router.route('/mainPage', {
    template: 'mainPage'
});

Documentation can be found here: https://github.com/iron-meteor/iron-router/blob/devel/Guide.md

Patricepatrich answered 25/5, 2015 at 20:26 Comment(3)
I have read it. I just can't find why it doesn't work... I input Router.go('mainPage') in Chrome console and It works perfect and return undefined.Pronucleus
As far as I can see your syntax for the events is not correct (Template.Post.events({})).Patricepatrich
If you can make a reproduction of the error, I can have a closer look.Patricepatrich

© 2022 - 2024 — McMap. All rights reserved.