在加载控制器之前验证路由前提条件 [英] Verifying route preconditions prior to loading controller

查看:27
本文介绍了在加载控制器之前验证路由前提条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Angular 编写一个单页应用程序,特别是 angular.dart,但我'假设这个问题仍然适用于 AngularJS.

I'm writing a single page application in Angular, specifically angular.dart, but I'm assuming this question still applies to AngularJS.

以以下路线为例:

/登录 -期望没有人登录.如果有人通过身份验证但未注册,则重定向到注册"路由,如果他们已注册,则重定向到家"路由.

/login - Expects nobody to be logged in. If someone is authenticated but not registered, redirect to "register" route, if they are registered, redirect to the "home" route.

/注册 -需要一个尚未完成注册过程的经过身份验证的用户.如果未通过身份验证,则重定向到登录.如果通过身份验证,则重定向到主页.

/register - Expects an authenticated user who hasn't finished the registration process. If not authenticated, redirect to login. If is authenticated, redirect to home.

/home -需要经过身份验证和注册的用户.如果未通过身份验证,则重定向到登录"路由,如果未注册,则重定向到注册"路由.

/home - Expects an authenticated and registered user. If not authenticated, redirect to "login" route, if not registered, redirect to "register" route.

我已经做了很多搜索,但找不到内置或惯用的方法来检查以确保在加载与特定路由关联的控制器之前满足某些先决条件,并在满足这些先决条件时进行适当的重定向没见过.

I've done quite a bit of searching but cannot find a built-in or idiomatic way to check to make sure that certain preconditions are met before loading the controller associated with a particular route, and to redirect appropriately when these preconditions are not met.

任何帮助将不胜感激!

推荐答案

Angular.Dart 和 Angular.JS 路由框架非常/根本不同.Angular.Dart 使用第三方路由框架(https://github.com/dart-lang/route/tree/experimental_hierarchical) 并且只实现了 Angular 特定的特性,比如 ng-view 和通过 ViewFactory 将路由绑定到模板的方法.

Angular.Dart and Angular.JS routing frameworks are very/fundamentally different. Angular.Dart is using a third party routing framework (https://github.com/dart-lang/route/tree/experimental_hierarchical) and only implements angular specific features like ng-view and ways to bind routes to templates via ViewFactory.

所以您的问题确实属于 route_hierarchical 包域.目前,您可以否决用户尝试离开路线的尝试,但我想您希望能够根据用户是否登录来否决用户输入路线.不幸的是,目前尚不支持,但已在计划中.

So your question really falls into the route_hierarchical package domain. Currently you can veto users attempt to navigate away from a route, but I guess what you want the ability to veto user entering a route depending on if the user is logged in or not. Unfortunately this is not yet supported, but is planned.

在此期间您可以尝试创建自定义 viewFactory 包装器.

What you could try in the meantime is creating a custom viewFactory wrapper.

class RecipesRouteInitializer implements RouteInitializer {
   void init(Router router, ViewFactory view) {
     router
       ..addRoute(
          name: 'login',
          path: '/login',
          enter: authView(router, view('login.html'), NOT_AUTH))
       ..addRoute(
          name: 'register',
          path: '/register',
          enter: authView(router, view('register.html'), AUTH_NO_REG))
       ..addRoute(
          name: 'home',
          path: '/home',
          enter: authView(router, view('home.html'), AUTH_AND_REG));
   }

   authView(Router router, ngView, mode) {
     return (RouteEvent e) {
       if (mode == AUTH_AND_REG) {
         if (!authService.isAuth) {
           router.go('login', {});
           return;
         }
         if (!authService.isReg) {
           router.go('register', {});
           return;
         }
       } else if (mode == AUTH_NO_REG) {
         if (!authService.isAuth) {
           router.go('login', {});
           return;
         }
         if (authService.isReg) {
           router.go('home', {});
           return;
         }
       } else if (mode == NOT_AUTH) {
         if (authService.isAuth) {
           router.go('register', {});
           return;
         }
       }
       ngView(e);
     };
   }

}

免责声明:这只是一个想法……可能完全行不通.

DISCLAMER: this is just an idea... might totally not work.

这篇关于在加载控制器之前验证路由前提条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆