简体   繁体   中英

jquery in didInsertElement jshint error

when using jquery inside the component callback, the callback function for click understands $ directly, and is working with $, but there is a jshint error

   components/xxx.js: line 13, col 17, '$' is not defined.

Using this.$ inside the jquery click callback gives an error at run time

import Ember from 'ember';

export default Ember.Component.extend({
    didInsertElement() {
        this._super(...arguments);

        this.$()
        .on('click', function() {
            $('.class').something(); //ok but jshint error
            this.$('.class').something();//jshint ok but error at run time
        });
    }
});

Thanks

在文件顶部使用它

/* globals $ */

Another approach is to set the following configuration to .jshintrc in the root of your app that prevents checking for the whole app which tells jshint that there are two global variables:

{
    "globals": {
        "$": false,
         "jQuery": false
    }
}

Note: If you like you can also use the jQuery version as same as Ember jQuery by changing

 $('.class').something(); //ok but jshint error

to

 Ember.$('.class').something(); 

which probably drops that error as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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