流星的铁路由器-路由前更改路径 [英] Meteor's Iron Router - Alter Path before routing
本文介绍了流星的铁路由器-路由前更改路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在基于某种逻辑(例如Session
变量)路由页面之前,是否可以更改路径?例如:
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');
}
});
推荐答案
您可以使用Iron.Location.get().path
获取当前路径,运行逻辑,然后在Router.go()
中使用新路径.像这样:
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);
}
,并确保您Session.set('abc') = false
在某个地方,否则它将继续循环,在无限循环中添加/abc
.
and make sure you Session.set('abc') = false
somewhere or else it will keep on looping, adding /abc
in an infinite loop.
这篇关于流星的铁路由器-路由前更改路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文