0
如果用戶尚未驗證,則會發生重定向到登錄頁面。 $state.go("login");
應該做到這一點,但它什麼都不做。當需要驗證時,ui路由器不會重定向
// in app.js
$stateProvider
.state('login', {
template: 'login.html'
url: '/login',
controller: 'AuthenticationCtrl',
})
.state('special', {
resolve: { loginRequired: checkAuthenticated },
template: 'special.html',
url: '/special',
controller: 'SpecialCtrl',
})
var checkAuthenticated = function($state, AuthenticationService, $q, $timeout) {
var deferred = $q.defer();
if(AuthenticationService.isAuthenticated()) {
deferred.resolve();
} else {
deferred.reject();
alert('You are being redirected to login page');
$state.go("login"); // redirect to login page -- but this does nothing.
}
}
在這裏,如果用戶沒有通過身份驗證,並試圖訪問要求認證的路線,我看到我的警報彈出。但事後沒有任何反應; ui-router
不會重定向到我的登錄頁面。
我也試過$location.path('/login')
而不是$state.go("login")
,但是這也沒有做任何事情。
我檢查說documentation爲go()
:
String Absolute State Name or Relative State Path
其中我有,所以我必須失去了一些東西是造成這個問題,但我不知道是什麼。
我遇到了一個問題,狀態與警報聯合起來,我通過使用超時設置爲100ms左右解決了這個問題,例如:延遲您的state.go,也許這將解決您的問題。 –
@ErikSvedin謝謝,使用'$超時'的作品。如果你做出答案,我會將其標記爲正確的。 – fanhats