Angular UI路由器转换HookMatchCriteria无法正常工作

时间:2017-01-16 10:38:35

标签: angularjs angular-ui-router transitions

我的运行块如下:

angular.module('app', [...]).run(['$transitions', '$state', 'EmployeeService', ($transitions, $state, EmployeeService) => {
    $transitions.onBefore(
        { to: 'app.*' },
        () => EmployeeService.checkAuth().then(() => {
            console.log("run success");
        },
        () => {
            console.log("run caught");
            return $state.target('login');
        }),
        {priority: 10}
    );
}])

由于某些原因,它会执行状态'app'和'login',虽然它们与'app。*'匹配条件不匹配,因此在执行注销时它会带来无限循环。指定匹配条件的正确语法是什么?我已经看过这里了:
https://ui-router.github.io/ng1/docs/latest/interfaces/transition.hookmatchcriteria.html

编辑:按要求发布路线配置 主模块的路由(包括不应该激活身份验证的'app'状态):

export default mainPageRoutesConfig;

function mainPageRoutesConfig($stateProvider, $urlRouterProvider, $locationProvider) {
    "ngInject";
    $locationProvider.html5Mode(true).hashPrefix('!');
    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('app', {
            url: '/',
            component: 'main'
        })
        .state('app.timesheet', {
            url: 'timesheet',
            component: 'timesheet'
        })
        .state('app.saveHours', {
            url: 'saveTaskHours',
            component: 'saveTaskHours',
            params: {
                timesheet:null
            }
        })
}

员工模块的路由(包括“登录”状态,也不应该启动身份验证检查):

export default employeesRoutesConfig;

function employeesRoutesConfig($stateProvider, $urlRouterProvider, $locationProvider) {
    "ngInject";

    $locationProvider.html5Mode(true).hashPrefix('!');
    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('login', {
            url: 'login',
            component: 'login'
        })  
        .state('app.employeesList', {
            url: 'employees',
            component: 'employeesList'
        })
        .state('app.saveEmployee', {
            url: 'employee/?Id',
            component: 'saveEmployee'
        })
        .state('app.deleteEmployee', {
            url: 'employee/:Id',
            component: 'deleteEmployee'
        })
        .state('app.employeePage', {
            url: 'profile/:Id',
            component: 'employeePage'
        })
}

1 个答案:

答案 0 :(得分:2)

解决方案(虽然不是真正的答案)是将匹配条件指定为函数:

        {
            to: function(state) {
                return state.name !== 'app.login' && state.name !== 'app.error';
            }
        }

完整的代码(移动到单独的文件后):

export default ($transitions, $state, EmployeeService) => {
    $transitions.onBefore(
        {
            to: function(state) {
                return state.name !== 'app.login' && state.name !== 'app.error';
            }
        },
        () => EmployeeService.checkAuth().then(() => {
            console.log("run block. Authorization check succeeded");
        },
        () => {
            console.log("run block. Authorization check failed");
            return $state.target('app.login');
        }),
        {priority: 10}
    );
}])