[英]Previous page location on IronRouter
有沒有辦法在轉到IronRouter的下一頁之前獲取上一頁的位置?
是否有可以用來獲取此信息的事件?
提前致謝。
由於Iron Router使用通常的History API,您可以使用普通的JS方法:
history.go(-1);
要么
history.back();
編輯:或檢查上一個路徑而不遵循它:
document.referrer;
您可以使用掛鈎實現所需的行為。
// onStop hook is executed whenever we LEAVE a route
Router.onStop(function(){
// register the previous route location in a session variable
Session.set("previousLocationPath",this.location.path);
});
// onBeforeAction is executed before actually going to a new route
Router.onBeforeAction(function(){
// fetch the previous route
var previousLocationPath=Session.get("previousLocationPath");
// if we're coming from the home route, redirect to contact
// this is silly, just an example
if(previousLocationPath=="/"){
this.redirect("contact");
}
// else continue to the regular route we were heading to
this.next();
});
編輯:這是使用iron:router@1.0.0-pre1
抱怨一個舊線程,但很好地保持這些東西最新saimeunt的答案現在已被棄用,因為在Iron Router中不再存在this.location.path所以應該類似於下面的內容:
Router.onStop(function(){ Session.set("previousLocationPath",this.originalUrl || this.url); });
或者如果您安裝了會話JSON(請參閱Session JSON )
Router.onStop(function(){ Session.setJSON("previousLocationPath",{originalUrl:this.originalUrl, params:{hash:this.params.hash, query:this.params.query}}); });
只有注意這一點,第一頁將始終填充url字段(this.url和this.originalUrl似乎沒有區別)與完整的URL( http:// .. ),而每個后續頁面只記錄相對域ie / home沒有root url不確定這是否是來自IR的預期行為,但它目前是確定這是否是第一頁加載的有用方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.