Denly
Denly

Reputation: 949

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.

Upvotes: 6

Views: 1658

Answers (4)

Denly
Denly

Reputation: 949

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.

Upvotes: 13

Philip Pryde
Philip Pryde

Reputation: 940

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

Upvotes: 5

Marius Darila
Marius Darila

Reputation: 873

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

Upvotes: 4

Fullhdpixel
Fullhdpixel

Reputation: 813

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

Upvotes: 0

Related Questions