Typescript and JQuery compile error: Cannot find name '$'

前端 未结 5 2112
醉梦人生
醉梦人生 2021-02-05 03:13

I\'m testing typescript with jquery, but when I compile the test.ts file, it always gives me an error indicating: Cannot find name \'$\'.

I\'ve already imported jquery &

相关标签:
5条回答
  • 2021-02-05 03:52

    You probably have some vulnerabilities you need to fix.

    Run npm audit fix to fix them, or npm audit for details.

    0 讨论(0)
  • 2021-02-05 03:58

    If you find these errors 90% of the time its because of versioning Problem of @types/jquery

    Try running:

     npm install jquery --save
    

    Then in app.module.ts:

    import * as $ from 'jquery';
    

    Then run:

      npm install @types/jquery@2.0.47
    

    And you should be ready to go.

    0 讨论(0)
  • 2021-02-05 04:05
    import * as $ from "jquery";
    

    didn't work for me. Interesting enough,

    import { ajax, css } from "jquery";
    

    actually solved my problem.

    0 讨论(0)
  • 2021-02-05 04:09

    I don't know if it helps but I solved the same issue for my datepicker.

    First I installed jQuery with this command: npm install --save-dev @types/jquery

    Then add the dependencies in your angular-cli.json file:

    "styles": [
                "../node_modules/bootstrap/dist/css/bootstrap.css",
                "../node_modules/font-awesome/css/font-awesome.css",
                "../node_modules/ng2-datetime/src/vendor/bootstrap-datepicker/bootstrap-datepicker3.min.css",
                "styles.css"
            ]
    
    "scripts": [
                "../node_modules/jquery/dist/jquery.js",
                "../node_modules/ng2-datetime/src/vendor/bootstrap-datepicker/bootstrap-datepicker.min.js"
            ]
    

    I think adding the jQuery part might work out for you. Please let me know if it works.

    0 讨论(0)
  • 2021-02-05 04:09

    Install and import following package. it will solved for error ("Cannot find name '$'"). Angular version is 7

    import 'datatables.net';

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