Web*_*nik 6 angularjs angular-ui-router
我正在升级ui-router@1.0.0-beta.2并尝试弄清楚如果用户没有必要的权限,将用户重定向到其他状态的最佳方法是什么.
这就是我所拥有的:
.state('company_dashboard', {
url: '/dashboard',
templateUrl: 'dashboard.html',
controller: 'CompanyDashboardCtrl',
resolve: {
user: function(UserService) {
return UserService.getActiveUser();
},
company: function(UserService) {
return CompanyService.getActiveCompany();
},
permissions: function(CompanyService, user, company){
return CompanyService.getUserPermissions(company, user);
}
},
onEnter: function($state, permissions) {
if (permissions.canAccessDashboard === false)
return $state.go('company_landing_page');
}
})
Run Code Online (Sandbox Code Playgroud)
上面的例子有效,但它记录了以下错误:TransitionRejection(type: 2, message: The transition has been superseded by a different transition, detail: Transition#1( ''{} -> 'company_landing_page'{} ))这让我觉得,应该有更好的方法.
边题,这是检查权限期间的好习惯resolve吗?
更新:
更改$state.go()以$state.target()帮助我摆脱控制台错误.这条评论有所帮助.虽然问题在于,我是否正确地做到了这一点?
小智 1
我将向您的状态添加一个数据属性,如下所示:
.state('company_dashboard', {
data: {
requiresAuthentication: true
}
}
Run Code Online (Sandbox Code Playgroud)
进而:
app.run(function ($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
// Check if user is authenticated
// And route requires authentication
if (!toState.data.requiresAuthentication) {
$state.go(toState.name, toParams);
} else if (user && toState.data.requiresAuthentication) {
$state.go(toState.name, toParams);
} else {
$state.go('login');
}
});
});
Run Code Online (Sandbox Code Playgroud)
即使我确信它可以更加完善,这似乎工作得很好,但我希望这能让您了解如何继续。
| 归档时间: |
|
| 查看次数: |
1832 次 |
| 最近记录: |