本文介绍了UI-Router 1.X - 如何处理解析函数抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以在单击按钮时转换到新状态,但是由于服务器上的一些错误,其中一个解析函数失败了.我应该如何以及在哪里处理这个异常.我已经尝试过此链接中描述的方法 https://github.com/angular-ui/ui-router/issues/1783,但它没有帮助.我不确定我是否做得对.这是我的代码从链接中获取后的样子,在设置断点并调试我的代码后,但我没有看到断点在解析函数中遇到异常.请纠正我在这里做错了什么或指导我朝着正确的方向解决这个问题.

I've a code to transition to new state on a button click, but one of the resolve functions is failing due to some error at the server. How and where should I handle this exception. I've tried the method described in this link https://github.com/angular-ui/ui-router/issues/1783, but it didn't help. I'm not sure if I'm doing it right or not. This is what my code looks like after taking it from the link, upon setting a break point and debug my code but I don't see the break point hitting on exception from the resolve function. Please correct me what am I doing wrong here or guide me in the right direction to get this resolved.

window.app = angular.module('app', ['di'])

app.run(['$rootScope',function($rootScope){
    $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
        if(toState.name == 'Categories')
        {
            // Do Something
        }
    });
}])

路由设置代码

{
    name: 'app.setting',
    url: '/settings',
    abstract: true,
    component: 'settingDetails',
    resolve: {
        getData: ($stateParams,
        $http)=>$stateParams.locationGroupId?$http.get(`/ui/getData/${
            $stateParams.id
        }/`).then(response=>response.data): [],
        
    }
}

推荐答案

在 UI-Router 1.X 中,状态更改事件已被弃用、禁用并由 Transition Hooks 取代.使用 Transition Hook API 代替监听事件.

With UI-Router 1.X, state change events are deprecated, DISABLED and replaced by Transition Hooks. Instead of listening for events, use the Transition Hook API.

有关更多信息,请参阅UI-路由器指南 - 1.0 迁移 - 状态更改事件

这篇关于UI-Router 1.X - 如何处理解析函数抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 05:06