在component.ts中找不到名称'$'

Man*_*ora 5 angular2-template angular

我收到错误:'在角度web-app的编译时,在组件'中找不到名字'$'.当我使用const $ : any = '';然后解决了错误但在浏览器控制台中得到另一个错误:'core.js:1440 ERROR TypeError:$不是函数'并且数据表在使用时也不起作用const $ : any = '';.

下面是我的用户组件代码.此组件用于数据表的用户列表.

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);
                }
            })
    }
}
Run Code Online (Sandbox Code Playgroud)

Ram*_*ran 10

  • 在控制台中首先安装jQuery

    const $: any = '';

  • 并安装jQuery Definition

    const $: any = '';

  • 在你的组件中调用你的jquery

    const $: any = '';

  • 并且不需要使用const $: any = '';,您应该删除此声明.

  • 我使用了declare var $: any; 而不是 import * as $ from 'jquery'; 现在它正在工作.. (2认同)

Ana*_*d G 5

我见过类似的,对我有用的解决方案是,

  1. 安装 jQuery

    npm install jquery --save

  2. 安装类型 jQuery

    npm install @types/jquery

  3. 将其导入您的模块

    //THis is important import * as $ from 'jquery';

希望这会有所帮助!