小编典典

什么是Angular ui路由器生命周期?(用于调试静默错误)

angularjs

我发现的最好的是http://www.ng-newsletter.com/posts/angular-ui-
router.html。它不会去深,例如,在该命令$stateChangeStartexampleState.onEnterexampleState.resolve,和exampleState.templateProvider火灾。

一个很好的答案格式将是干净的。就像是:

  1. 状态foo的初始页面加载:

    1. 角生命周期步骤1
    2. UI路由器生命周期步骤1
    3. UI路由器生命周期解决
    4. UI路由器生命周期onEnter触发
    5. 角生命周期步骤2
    6. 状态更改 foo- > bar

    7. $stateChangeStart 事件引发

    8. foo onExit
    9. bar onEnter火灾
    10. templateUrl 获取模板
    11. UI路由器在摘要循环(或任何位置)中重新插入Angular生命周期。
    12. 嵌套状态
  2. 多个命名视图:

  3. ui-sref单击

等等…谢谢!


阅读 324

收藏
2020-07-04

共1个答案

小编典典

经过一些试验,我弄清楚了如何充分了解生命周期,以调试我的应用程序并了解正在发生的事情。使用所有事件(包括onEnter,onExit,stateChangeSuccess和此处的
viewContentLoaded),可以使我对事件的发生情况有一个体面的了解,这种方式对代码的灵活性和针对性比书面生命周期更加灵活和具体。在应用程序模块的“运行”功能中,我放置了:

如果我第一次使用Angular和UI-router时就开始使用它,那么这段代码将节省我很多时间和混乱。UI路由器需要“调试”模式,默认情况下启用此功能。

$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
  console.log('$stateChangeStart to '+toState.name+'- fired when the transition begins. toState,toParams : \n',toState, toParams);
});
$rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams, error){
  console.log('$stateChangeError - fired when an error occurs during transition.');
  console.log(arguments);
});
$rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
  console.log('$stateChangeSuccess to '+toState.name+'- fired once the state transition is complete.');
});
$rootScope.$on('$viewContentLoading',function(event, viewConfig){
   console.log('$viewContentLoading - view begins loading - dom not rendered',viewConfig);
});

/* $rootScope.$on('$viewContentLoaded',function(event){
     // runs on individual scopes, so putting it in "run" doesn't work.
     console.log('$viewContentLoaded - fired after dom rendered',event);
   }); */

$rootScope.$on('$stateNotFound',function(event, unfoundState, fromState, fromParams){
  console.log('$stateNotFound '+unfoundState.to+'  - fired when a state cannot be found by its name.');
  console.log(unfoundState, fromState, fromParams);
});
2020-07-04