Emberjs:有条件过渡到路线

问题描述 投票:0回答:1

我正在尝试在router.js中进行条件转换。我需要根据另一个休息服务返回的布尔值来确定条件(哪个是合格)。我可以编写一个函数来确定router.js中的值以及如何使用该值将我的转换重定向到两个单独的路由。如果eligibity为真,我需要做'this.route('coverage',{path:'/'});否则this.route('inEligible',{path:'/'}); “请举个例子。我很贪图。

ember.js
1个回答
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;
    }
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.