Meteor IronRouter上的上一页位置

Meteor IronRouter上的上一页位置,meteor,iron-router,Meteor,Iron Router,在IronRouter中转到下一页之前,是否有方法获取上一页的位置 是否存在可用于获取此信息的事件 提前感谢。由于Iron Router使用了常用的历史API,因此您可以使用普通JS方法: history.go(-1); 或 编辑:或检查上一个路径而不跟随它: document.referrer; 您可以通过使用挂钩来实现所需的行为 // onStop hook is executed whenever we LEAVE a route Router.onStop(function(){

在IronRouter中转到下一页之前,是否有方法获取上一页的位置

是否存在可用于获取此信息的事件


提前感谢。

由于Iron Router使用了常用的历史API,因此您可以使用普通JS方法:

history.go(-1);

编辑:或检查上一个路径而不跟随它:

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

编辑:这是使用
熨斗:router@1.0.0-pre1

很抱歉撞到了一个旧线程,但最好让这些内容保持最新Saimemount的上述回答现在已被弃用,因为Iron Router中不再存在this.location.path,因此应该类似于以下内容:


Router.onStop(函数(){
set(“previousLocationPath”,this.originalUrl | | this.url);
});

或者如果已安装会话JSON(请参阅)


Router.onStop(函数(){
setJSON(“previousLocationPath”,{originalUrl:this.originalUrl,params:{hash:this.params.hash,query:this.params.query});
});


唯一需要注意的是,第一页将始终使用完整url(.)填充url字段(this.url和this.originalUrl,两者之间似乎没有区别)虽然每个后续页面只记录相对域,即/home,而没有根url,不确定这是否是预期行为或不是来自IR,但它目前是确定这是否是第一个页面加载的一种有用方法

您可以使用历史API:
文档。referer
仅提供您到达的初始位置从中,它没有被HTML5历史API更新。此方法与sAlert有问题,以防您希望为下一条路由保留消息。此方法还进行回发
// 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();
});