Angular函数未使用显式注释,因此无法在严格模式下调用 [英] Angular function is not using explicit annotation and cannot be invoked in strict mode

查看:161
本文介绍了Angular函数未使用显式注释,因此无法在严格模式下调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my web application I want to verify if the user has the permissions to continue.

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

This is the .run method in my index.js file:

  .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

推荐答案

您启用了严格模式,以检测您忘记了使用$ inject或数组表示法来确保可以安全地缩小代码的位置.

You enabled strict mode, in order to detect where you forgot to use $inject or the array notation to make sure your code can be safely minified.

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

The message tells you that you failed to properly annotate your injectable functions. Note that, in addition to that, your code doesn't make much sense:

  • 传递给run()的函数应该以可注入服务作为参数,除$ window以外的所有参数都不是服务.
  • 传递给$ on()的函数不是可注入函数.因此,将其传递给$ windows毫无意义.路由器将仅使用前四个参数来调用它.

因此,您的代码应为:

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

我强烈建议您停止手动注释可注入函数,并使用 ng-annotate 为您做到这一点.

I would strongly advise to stop annotating your injectable functions by hand, and to use ng-annotate to do that for you.

这篇关于Angular函数未使用显式注释,因此无法在严格模式下调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆