本文介绍了Angular-注销重定向路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一条清除用户会话并将其重定向回根主页的路由.
I am trying to create a route that clears the users session and redirects them back to the root homepage.
.config(function config( $routeProvider, $stateProvider ) {
$routeProvider.
when('/logout', {resolve: {redirect: function(Session){
Session.clear();
return "/home";
}}});
我显然在打电话时做错了事
I'm obviously doing something wrong here, as calling
$location.path("/logout");
...忽略该功能,然后重定向回默认路由.我尝试将console.log语句添加到函数中,以查看是否正在调用它.
... ignores the function and redirects back to the default route. I've tried adding console.log statements to the function to see if it is being called.
我是否正确使用了重定向功能?
Am I using the redirect function incorrectly?
推荐答案
您是否考虑过将注销逻辑放在单独的控制器中?会更清洁,更坚固和更安全.使重定向更直接.像这样:
Have you considered putting your logout logic in a separate controller? Would be a little cleaner, more robust, & make redirection more straightforward. Like so:
function LogoutController($location) {
Session.clear();
$location.path('/home');
}
您的路线是:
when('/logout', {
template: '', //A template or templateUrl is required by AngularJS, even if your controller always redirects.
controller: 'LogoutController'
}).
这篇关于Angular-注销重定向路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!