gpt4 book ai didi

angularjs - UI-Router 1.0 基于用户 Angular 色重定向

转载 作者:行者123 更新时间:2023-12-02 23:43:17 26 4
gpt4 key购买 nike

我正在升级到 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');
}
})

上面的示例有效,但它记录了以下错误:TransitionRejection(type: 2, message: The conversion has was superseded by a differenttransition,detail: Transition#1( ''{} -> 'company_landing_page '{} )) 这让我想,应该有更好的方法。

附带问题,在解决期间检查权限是一个好的做法吗?

更新:

$state.go() 更改为 $state.target() 帮助我摆脱了控制台错误。 This comment有帮助。尽管问题依然存在,但我是否在正确的地方做这件事?

最佳答案

我将向您的州添加一个数据属性,如下所示:

.state('company_dashboard', {
data: {
requiresAuthentication: true
}
}

然后:

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

这似乎工作得很好,即使我确信它可以更加完善,但我希望这能让您了解如何继续。

关于angularjs - UI-Router 1.0 基于用户 Angular 色重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39475916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com