ember.js 有没有办法找出它是从哪条路线过渡到目前的路线在ember?

mcdcgff0  于 2022-11-05  发布在  其他
关注(0)|答案(3)|浏览(134)

假设我们有3条路线:A、B和C
有可能从A过渡到C,或从B过渡到C

A -> C
B -> C

在路线C中,我想知道它是从哪条路线过渡过来的。要么是A,要么是B。有没有简单的方法找到它?
P.S.我想做的是在从A(或B)转换到C时添加一个带有路由名称(A或B)的额外参数,并在路由C的beforeModel钩子上获得该参数。

3zwtqj6y

3zwtqj6y1#

可以通过以下方式设置查询参数:控制器/路由并在路由中访问它们

beforeModel:function(transition){
   transition.queryParams
}

因此,您可以从a将查询参数设置为A,并根据A在beforeModel中执行所需的操作。对于B也是如此

toiithl6

toiithl62#

基于How to get current routeName?
我添加了一个previousRoute属性。这比使用查询参数要容易。
解决方法:

App = Ember,Application.create({
      currentPath: '',
      previousPath: null
 });

ApplicationController : Ember.Controller.extend({
  currentPathDidChange: function() {
      App.set('previousPath', App.get('currentPath'));
      App.set('currentPath', this.get('currentPath'));
  }.observes('currentPath')
});

可以使用以下方法访问这些属性:

App.get('currentPath');
App.get('previousPath');
piok6c0g

piok6c0g3#

在Emberv3.6中,from属性已新增至Transition类别。此属性是RouteInfo对象,代表转场的来源。对于初始呈现,此值会设定为null

// routes/b.js

// transition from A --> B
beforeModel(model, transition) {
    console.log(transition.from); // A;
}

链接到文档:https://api.emberjs.com/ember/3.6/classes/Transition/properties/from?anchor=from

相关问题