Emberjs:有条件地过渡到路线

Emberjs: conditional transition to route

我正在尝试在 router.js 中进行条件转换。 我需要根据另一个休息服务返回的布尔值来确定条件(即资格)。 我能否编写一个函数来确定 router.js 中的值以及如何使用该值将我的转换重定向到两个单独的路由。 如果资格是真的,我需要做 ' this.route('coverage', { 小路: '/' }); 别的 this.route('inEligible', { 小路: '/' }); ' 请给我一个例子。我对 ember 很陌生。

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

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