jslint错误:意外表达'使用严格'声明中的立场

时间:2016-12-27 17:22:45

标签: javascript angularjs jslint

当我尝试在崇高文本中保存以下代码时,

'use strict';
 /*global angular,_*/

 var app = angular.module("myApp", []);
 app.controller("myCtrl", function ($scope) {
   $scope.firstName = "John";
   $scope.lastName = "Doe";
 });

我收到以下jslint错误:

 #1 Unexpected expression 'use strict' in statement position.
    'use strict'; // Line 1, Pos 1
 #2 Place the '/*global*/' directive before the first statement.
    /*global angular,_*/ // Line 2, Pos 1
 #3 Undeclared 'angular'.
    var app = angular.module("myApp", []); // Line 4, Pos 11
 #4 Expected 'use strict' before '$scope'.
    $scope.firstName = "John"; // Line 6, Pos 3
 #5 Expected '$scope' at column 5, not column 3.
    $scope.lastName = "Doe"; // Line 7, Pos 3

1 个答案:

答案 0 :(得分:3)

您不能使用jslint以全局方式使用'use strict';。见https://stackoverflow.com/a/35297691/1873485

考虑到这一点,您需要将其从全局范围中删除并将其添加到您的函数范围中或将所有内容包装在IFEE中

 /*global angular,_*/
 var app = angular.module("myApp", []);
 app.controller("myCtrl", function ($scope) {
  'use strict';
   $scope.firstName = "John";
   $scope.lastName = "Doe";
 });

或包装它:

/*global angular,_*/
(function(){
  'use strict';
   var app = angular.module("myApp", []);
   app.controller("myCtrl", function ($scope) {
     $scope.firstName = "John";
     $scope.lastName = "Doe";
   });
})();