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

EDIT:这是使用iron:router@1.0.0-pre1

很抱歉撞到了一个旧线程,但很高兴能让这些东西保持最新saimeunt上面的答案现在被否决了,因为this.location.path在Iron Router中不再存在,所以应该类似于下面的内容:

Router.onStop(function(){ Session.set("previousLocationPath",this.originalUrl || this.url); });

或者,如果您安装了会话JSON(请参阅会话JSON)

Router.onStop(function(){ Session.setJSON("previousLocationPath",{originalUrl:this.originalUrl, params:{hash:this.params.hash, query:this.params.query}}); });

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

最新更新