在加载控制器之前验证路由前提条件 [英] Verifying route preconditions prior to loading controller
问题描述
我在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 -
预期没有人登录。如果有人已通过身份验证但未注册,则重定向到注册路线,如果他们已注册,则重定向到主页路线。
/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 -
预期未完成注册过程的已验证用户。如果没有验证,重定向到登录。如果已验证,请重定向到首页。
/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 ),只实现角度特定的功能,如 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屋!