Meteor's Iron Router - Alter Path before routing

旧街凉风 提交于 2019-12-12 01:47:06

问题


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:


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.



来源:https://stackoverflow.com/questions/27977244/meteors-iron-router-alter-path-before-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!