Meteor's Iron Router - Alter Path before routi

2019-09-07 22:16发布

Is there a way to change the path before the page is routed based on some logic, such as a Session variable? For example:

// Before routing
Router.onBeforeAction(function () {
    if(Session.get('key') === true) {
        prependToPath('prefix');
    }
});

1条回答
贪生不怕死
2楼-- · 2019-09-07 22:41

You can get the current path using Iron.Location.get().path, run through your logic, and then use the new path in Router.go(). Like so:

// If abc is set on the URL, then keep it there
if (Session.get('abc') === true) { // You can use better logic here
    Router.go('/abc' + Iron.Location.get().path);
}

and make sure you Session.set('abc') = false somewhere or else it will keep on looping, adding /abc in an infinite loop.

查看更多
登录 后发表回答