Angular 函数未使用显式注解,无法在严格模式下调用

Angular function is not using explicit annotation and cannot be invoked in strict mode

在我的 Web 应用程序中,我想验证用户是否有权继续。

这是我的 index.js 文件中的 .运行 方法:

  .run(function (event, toState, toParams, fromState, $window) {
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, $window) {
  if (Auth.isLoggedIn && $window.localStorage.identityToken) {

    var requiredAdmin = toState.data.requiredAdmin; 
    if (requiredAdmin && $window.localStorage.isAdmin){
      $state.go(toState.name);
    } else {
      $state.go(fromState.name);
    }
    var shouldGoToMain = fromState.name === 'login' && toState.name !== 'app.dashboard' ;

    if (shouldGoToMain){
      $state.go('app.dashboard');
      event.preventDefault();
    } else {
      $state.go(toState.name);
    }
    return;
  } else { 
    $state.go('login');
    return;
  }

  // unmanaged
});

});

控制台错误是:Uncaught Error: [$injector:strictdi] function(event, toState, toParams, fromState, $window) is not using explicit annotation and cannot be invoked in strict mode

您启用了 strict mode,以检测您忘记使用 $inject 或数组符号的位置,以确保您的代码可以安全地缩小。

该消息告诉您未能正确注释可注入函数。请注意,除此之外,您的代码没有多大意义:

  • 传递给 运行() 的函数应该将可注入服务作为参数,除 $window 之外的所有参数都不是服务。
  • 传递给 $on() 的函数不是可注入函数。所以传递它 $windows 是没有意义的。路由器只会用前 4 个参数调用它。

所以,您的代码应该是:

.run(['$rootScope', '$window', function($rootScope, $window) {
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState) { 

我强烈建议停止手动注释您的可注入函数,并使用 ng-annotate 为您完成。