I want to use jquery in my Angular-TypeScript project. So I installed jquery like "npm install @type/jquery". Now when I try to serve it says:
error TS2592: Cannot find name '$'. Do you need to install type definitions for jQuery? Try npm i @types/jquery
and then add jquery
to the types field in your tsconfig.
How do I add jquery to the types field?
My Code:
let settings = {
"async": true,
"crossDomain": true,
"url": "https://api.com/getSomething",
"method": "POST",
"headers": {
"x-rapidapi-host": "xxx",
"x-rapidapi-key": "xxx",
"content-type": "xxx"
},
"data": {}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
It's very well explained in the error message. If you want to use TS, and use jQuery, you'll need the typings for jQuery. You can achieve this by running npm i @types/jquery
in your project.
You will then need to add "jquery"
in the types
array in your tsconfig.app.json
file, in the Angular application (might depend on your Angular version - best to check in the documentation).
Might I add - if you're using Angular, I'd say it's best not to use jQuery. Angular is already capable of doing everything. If you'd like to use jQuery, having an entire Angular application with it is a bit unnecessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With