JSHint和grunt.js - 未定义Highcharts

时间:2014-09-16 11:29:20

标签: javascript angularjs highcharts gruntjs jshint

我正在使用Angular应用程序,一旦我对我运行grunt.js的所有内容感到高兴,就可以在所有内容都缩小的情况下构建分发版,现在我的问题就开始了。当文件没有缩小时,所有Highcharts工作正常,一旦我缩小了js文件,我得到错误Highcharts没有定义。

highcharts的部分代码,我得到提示有问题,请参阅错误评论:

function createDevicesChart (title) {
    Highcharts.setOptions ({ //error Highcharts is not defined, unresolved variable Highcharts, unresolved function setOptions
        lang: {rangeSelectorZoom: ''},
        colors: ['#F4D00B']
    });
    Highcharts.RangeSelector.prototype.render = (function (func) { //error Highcharts is not defined, unresolved variable Highcharts, unresolved variable RangeSelector
        return function () {
            func.apply (this, arguments);
            var leftPosition = this.chart.plotLeft,
            topPosition = this.chart.plotTop,
            space = 1;
            var widthChart = this.chart.chartWidth;
            var widthChartHolder = $ ('.highcharts-container >svg').width ();
            //console.log(widthChartHolder);
            for (var i = 0; i < this.buttons.length; i++) {
                this.buttons[i].attr ({
                    x: widthChartHolder - leftPosition - 41,
                    y: topPosition - 77
                });
                leftPosition += this.buttons[i].width + space;
            }
        };
    } (Highcharts.RangeSelector.prototype.render));//error Highcharts is not defined, unresolved variable RangeSelector

        .........//rest of the code 


    angular.element('#devices_chart').highcharts ('StockChart', chartingOptions); //unresolved function or method highcharts()

}

.jshintrc文件:

    {
      "node": true,
      "browser": true,
      "esnext": true,
      "bitwise": true,
      "camelcase": true,
      "curly": true,
      "eqeqeq": true,
      "immed": true,
      "indent": 2,
      "latedef": true,
      "newcap": true,
      "noarg": true,
      "quotmark": "single",
      "regexp": true,
      "undef": true,
      "unused": true,
      "strict": true,
      "trailing": true,
      "smarttabs": true,
      "globals": {
        "angular": false,
        "Highcharts"  : false
      }

}

有人可以帮帮我吗? 我真的在这里挣扎:( 非常感谢

编辑:即使我为Highcharts更新了jshintr的false值,当我运行grunt job时它会缩小所有代码并且我再次在控制台中收到错误:

ReferenceError: Highcharts is not defined
    at m (http://localhost/myAPP/dist/scripts/scripts.js:1:3021)
    at link (http://localhost/myAPP/dist/scripts/scripts.js:1:3125)
    at http://localhost/myAPP/dist/scripts/vendor.js:4:20180
    at s (http://localhost/myAPP/dist/scripts/vendor.js:4:14727)
    at h (http://localhost/myAPP/dist/scripts/vendor.js:4:10779)
    at h (http://localhost/myAPP/dist/scripts/vendor.js:4:10796)
    at http://localhost/myAPP/dist/scripts/vendor.js:4:10402
    at http://localhost/myAPP/dist/scripts/vendor.js:7:19333
    at s (http://localhost/myAPP/dist/scripts/vendor.js:4:14727)
    at h (http://localhost/myAPP/dist/scripts/vendor.js:4:10779) <div id="devices_chart" class="ng-isolate-scope"> 

2 个答案:

答案 0 :(得分:1)

你必须告诉jshint如何处理这样的全局变量。

在项目的根目录中创建一个.jshintrc配置文件,其中包含:

{
    // JSHint Default Configuration File (as on JSHint website)
    // See http://jshint.com/docs/ for more details

    //[...] // global conf ... see docs

    // Custom Globals
    "globals" : {
        "Highcharts"  : false //here is what you need
    }
}

请参阅http://jshint.com/docs/

答案 1 :(得分:0)

这是Grunt配置中的jshintrc选项,你可能错过了。

grunt.initConfig({
    jshint: {
        options: {
            jshintrc: true
        }
    }
});