IronRouter上的上一页位置

Luc*_*uca 5 meteor iron-router

有没有办法在转到IronRouter的下一页之前获取上一页的位置?

是否有可以用来获取此信息的事件?

提前致谢.

Hub*_* OG 9

由于Iron Router使用通常的History API,您可以使用普通的JS方法:

history.go(-1);
Run Code Online (Sandbox Code Playgroud)

要么

history.back();
Run Code Online (Sandbox Code Playgroud)

编辑:或检查上一个路径而不遵循它:

document.referrer;
Run Code Online (Sandbox Code Playgroud)


sai*_*unt 3

您可以通过使用钩子来实现您想要的行为。

// 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();
});
Run Code Online (Sandbox Code Playgroud)

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