jQuery is not defined for angular 2

前端 未结 3 647
猫巷女王i
猫巷女王i 2020-12-15 10:08

I am trying to use ng2-datetime. However, when I follow the instruction and installed npm install --save ng2-datetime, I get the following error.

I have tri

相关标签:
3条回答
  • 2020-12-15 10:42

    Wasted a good amount of time, here is what I did and didn't help.

    npm uninstall -g angular-cli
    npm uninstall -g @angular/cli
    npm cache clear --force
    npm cache clean
    npm install -g @angular/cli
    

    and all other usual things,

    • clear browser cache
    • restart browser
    • kill and re-run ng serve

    In my case I had to do the following:

    npm install --save @types/jquery
    

    Import jquery in the .angular-cli.json

    "scripts": [
         "../node_modules/jquery/dist/jquery.js"
    ],
    

    change code from

    import $ from 'jquery'; 
    

    to

    import * as jquery from 'jquery';
    

    note: issue started after angular upgrade from 4.2 to 5.2

    0 讨论(0)
  • 2020-12-15 10:54

    You need to import jquery with angular-cli. Edit your angular-cli.json file.

    Find script array and add jquery.

    "scripts": [
         "../node_modules/jquery/dist/jquery.min.js"
    ],
    
    0 讨论(0)
  • 2020-12-15 10:56

    import * as $ from 'jquery';

    window['$'] = window['jQuery'] = $;

    The sencond line will fix your issue!

    0 讨论(0)
提交回复
热议问题