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

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.



Solution 1:[1]

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.

Solution 2:[2]

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');
}

Solution 3:[3]

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); 
}

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Community
Solution 3 Marius Darila
Solution 4 Fullhdpixel