简体   繁体   English

使用浏览器导航器会导致JShint错误

[英]Using browsers navigator results in JShint error

I'm using the globally available navigator object that the browser exposes in an Ember-CLI project (aka, with ES6 syntax) and I'd like to avoid getting errors when referencing this valid global object. 我正在使用浏览器在Ember-CLI项目(也称为ES6语法)中公开的全局可用navigator器对象,并且我想避免在引用此有效全局对象时出现错误。

I saw this suggestion: Ember CLI - Error when using moment.js in route and tried added ['navigator'] to the predef definition in the .jshintrc file. :我看到这个建议使用航线moment.js时的错误-灰烬CLI ,并试图加入[“导航”]到predef在定义.jshintrc文件。 Didn't seem to have any impact. 似乎没有任何影响。 I also then put the following into the JS file itself: 然后,我还将以下内容放入JS文件本身:

/* global navigator: false */

That worked but I still feel the .jshintrc alternative would be nice. 那行得通,但我仍然觉得.jshintrc替代方案会很好。 Is there a way to achieve this? 有没有办法做到这一点? Why didn't my attempt have the desired results in the same way that the momentjs example did? 为什么我的尝试没有像moment.js示例那样获得期望的结果?


BTW, here is the default setting that Ember-CLI puts in: 顺便说一句,这是Ember-CLI的默认设置:

"predef": [
    "document",
    "window",
    "-Promise"
]

The existing answer isn't wrong, exactly, but it does have the wrong format, and doesn't make clear that there is a seperate .jshintrc file for tests. 确实存在的答案没有错,但是格式确实不正确,也不清楚是否有单独的.jshintrc文件用于测试。 In my case, this was the one that needed to be updated. 就我而言,这是需要更新的。

In tests/.jshintrc (which is different from the main .jshintrc ) add "navigator" to the "predef" array, like so: tests/.jshintrc (与主要.jshintrc不同)中,将"navigator"添加到"predef"数组,如下所示:

{
  "predef": [
    "document",
    "window",
    "navigator",
    "location",
    ...
  ]
}

In .jshintrc file, you should do it that way: .jshintrc文件中,您应该这样做:

{
  "predef": {
    "navigator": true
  }
}

Hope this helps! 希望这可以帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM