下面这段代码中 ngInject 做了什么?

2024-03-08

AngularJS 控制器代码:

function AuthConfig($stateProvider, $httpProvider) {
  'ngInject';

  // Define the routes
  $stateProvider

  .state('app.login', {
    url: '/login',
    templateUrl: 'auth/auth.html',
    title: 'Sign in'
  })

  .state('app.register', {
    url: '/register',
    templateUrl: 'auth/auth.html',
    title: 'Sign up'
  });

};

export default AuthConfig;

我无法弄清楚 ngInject 有什么用。有人可以帮我吗?


'ngInject';它本身什么也不做,它只是一个字符串文字。一个名为 ng-annotate 的工具将其用作标志:如果函数以'ngInject';,它将被 ng-annotate 处理。

基本上,ng-annotate会转变

angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) {
    "ngInject";
    ...
});

to

angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", function($scope, $timeout) {
    "ngInject";
    ...
}]);

为了使代码缩小安全。

如果您不使用 ng-annotate,则可以安全地忽略或删除该表达式。但要小心,如果项目确实使用了 ng-annotate,您可能会破坏项目的构建过程。 有关 ng-annotate 及其用途的更多信息,请参阅https://github.com/olov/ng-annotate https://github.com/olov/ng-annotate

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

下面这段代码中 ngInject 做了什么? 的相关文章

随机推荐