I got error:'Cannot find name '$' in component' on compile time of angular web-app. When i used const $ : any = '';
then solved error but getting a another error in browsers console: 'core.js:1440 ERROR TypeError: $ is not a function' and also datatable not working when used const $ : any = '';
.
Below is my code of users component. This component is used for list of users into datatable.
users.component.ts
import {DataService} from './../services/data.service';
import {Component, OnInit} from '@angular/core';
import {API_ENDPOINT} from '../app.module';
import {Response} from '@angular/http/src/static_response';
const $: any = '';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.css']
})
export class UsersComponent implements OnInit {
private url = API_ENDPOINT + '/admin_api/users';
private users: any;
constructor(private dataService: DataService) {
}
ngOnInit() {
this.dataService.get(this.url)
.subscribe(responce => {
this.users = responce.data.users;
if (this.users) {
setTimeout(function () {
var oTable1 = $('#sample-table-2').dataTable({
"aoColumns": [
{"bSortable": false},
null, null, null, null, null,
{"bSortable": false}
],
});
}, 3000);
}
})
}
}
npm install --save jquery
npm install -D @types/jquery
import {DataService} from './../services/data.service';
import {Component, OnInit} from '@angular/core';
import {API_ENDPOINT} from '../app.module';
import {Response} from '@angular/http/src/static_response';
import * as $ from 'jquery';// import Jquery here
.
.
your code
.
.
const $: any = '';
, You should remove this declaration.I have seen similar and solution which worked for me was,
Install jQuery
npm install jquery --save
Install type jQuery
npm install @types/jquery
Import it into your module
//THis is important
import * as $ from 'jquery';
Hope this will help!
install jQuery
npm install --save jquery
And install jQuery Definition
npm install -D @types/jquery
** than include jquery in component like this **
import * as $ from jquery
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