【问题标题】:ERROR ReferenceError: $ is not defined at angular datatables [duplicate]错误ReferenceError:$未在角度数据表中定义[重复]
【发布时间】:2019-03-30 12:53:33
【问题描述】:

我正在尝试将分页和排序添加到我的表中,但出现此错误,但是我遵循此处列出的所有步骤 http://l-lin.github.io/angular-datatables/#/getting-started.

我已经检查了之前的问题,但我没有和我一起工作

我安装了它的所有依赖项

这是组件的代码:-

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ProductService } from '../../service/product-service.service';
import { Subscription, Subject } from 'rxjs';



@Component({
  selector: 'app-admin-products',
  templateUrl: './admin-products.component.html',
  styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, OnDestroy {
  

  products: any[];
  filteredProducts: any[];
  subscribtion: Subscription;
  dtOptions: DataTables.Settings = {};
  dtTrigger: Subject<any> = new Subject();

  constructor(private productService: ProductService) {
    this.subscribtion = productService.getAll().
    // We take a copy of Products and Assigned to filteredProducts
    subscribe(
      products => {
      this.filteredProducts = this.products = products;
      this.dtTrigger.next();
      }
      );
  }

  ngOnInit() {
    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 5,
      processing: true
    };

  }
  filter(queryStr: string) {
    // console.log(this.filteredProducts);
    if (queryStr) {
      this.filteredProducts = this.products.
      filter(p => p.payload.val().title.toLowerCase().includes(queryStr.toLowerCase()));
    } else {
      this.filteredProducts = this.products;
    }
  }

  ngOnDestroy(): void {
    // to UnSubscribe
    this.subscribtion.unsubscribe();
  }

}

这是 HTML 的代码:-
我也按照这里的所有步骤

<p>
    <a routerLink="/admin/products/new" class="btn btn-primary">New Product</a>
</p>

<p>
   <input type="text"
    #query
    (keyup)="filter(query.value)"
   placeholder="Search ..." class="form-control">
</p>
<table 
datatable [dtOptions]="dtOptions"
[dtTrigger]="dtTrigger" class="table" >
    <thead class="thead-dark">
      <tr>
        <th scope="col">Title</th>
        <th scope="col">Price</th>
        <th scope="col">Edit</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let product of filteredProducts">
       
        <td>{{ product.payload.val().title }}</td>
        <td>{{ product.payload.val().price }}</td>
        <td>
            <a [routerLink]="['/admin/products/', product.key]">Edit</a>
        </td>
      </tr>
    </tbody>
  </table>

【问题讨论】:

  • 听起来你不包括 jQuery....
  • 如果您还没有加载 jQuery,您可能会问自己,DataTables 是否是正确的选择。还有其他选项不需要其他库。
  • 我已经在 angular.json 中添加了这个脚本 "styles": [ "node_modules/datatables.net-dt/css/jquery.dataTables.css" ], "scripts ": [ "node_modules/jquery/dist/jquery.js", "node_modules/datatables.net/js/jquery.dataTables.js" ],
  • 在 polyfill.ts 中添加 import * as jQuery from 'jquery'; window['$'] = jQuery;

标签: javascript jquery html angular


【解决方案1】:

$ 未定义主要意味着您不包括 JQuery。

尝试将:添加到您的程序中

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js" type="text/javascript"></script>

source

【讨论】:

  • 我已经试过了,但是不行
  • 它现在可以工作,但是为什么当我在 Index.html 中添加它而不是在 angular.json 中工作时它可以工作
  • 在 Angular 中你使用 JQuery,如果在 Angular 中做某事之前没有定义 JQuery,它将无法工作,因为它没有定义。
  • 这不起作用。尝试这些步骤- 1. npm install jquery @types/jquery --save 2. 转到项目中的 angular.json,在脚本中添加“node_modules/jquery/dist/jquery.min.js”:[]。 3. 重新加载项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多