Emberjs:有条件地过渡到路线

时间:2019-03-06 19:50:08

标签: ember.js

我正在尝试在router.js中进行条件转换。 我需要根据另一个休息服务返回的布尔值确定条件(哪个是合格对象)。 我可以编写一个函数来确定router.js中的值,以及如何使用该值将转换重定向到两条单独的路由。 如果资格是真的,我需要做 ' this.route('coverage',{     路径:“ /”   }); 其他 this.route('inEligible',{     路径:“ /”   }); ' 请提供一个例子。我是炭烬新手。

1 个答案:

答案 0 :(得分:3)

我在路线中使用了这种模式:

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';


export default class SettingsRoute extends Route {
  @service currentUser;

  async beforeModel() {
    // inside the service, I have logic for checking if the user
    // authenticated, not expired, and fetched
    const isLoggedIn = await this.currentUser.isLoggedIn();

    if (!isLoggedIn) {
      this.transitionTo('setup');
      return;
    }
  }
}

如果您使用的是非本地类:

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';


export default Route.extend({
  currentUser: service();

  async beforeModel() {
    // inside the service, I have logic for checking if the user
    // authenticated, not expired, and fetched
    const isLoggedIn = await this.currentUser.isLoggedIn();

    if (!isLoggedIn) {
      this.transitionTo('setup');
      return;
    }
  }
});
相关问题