我正在尝试在我的 Angular 6 项目中使用 jQuery 和 jsGrid,它不会编译,除非我评论有问题的代码部分,然后在编译时我评论并且它可以工作,但无论如何它都会出错。我使用的是 Angular 6 CLI,IDE 是 Visual Studio Code,所有最新版本。VS 突出显示 jQuery 别名 ie $ 符号作为问题
这是 structure.directive.ts(我必须注释整个 ngOnInit 才能启动服务器,然后取消注释):
import { Directive, ElementRef, OnInit } from '@angular/core';
@Directive({
  selector: '[structure]'
})
export class StructureDirective implements OnInit {
  clients = [
    { "Name": "Otto Clay", "Age": 25, "Country": 1, "Address": "Ap #897-1459 Quam Avenue", "Married": false },
    { "Name": "Connor Johnston", "Age": 45, "Country": 2, "Address": "Ap #370-4647 Dis Av.", "Married": true },
    { "Name": "Lacey Hess", "Age": 29, "Country": 3, "Address": "Ap #365-8835 Integer St.", "Married": false },
    { "Name": "Timothy Henson", "Age": 56, "Country": 1, "Address": "911-5143 Luctus Ave", "Married": true },
    { "Name": "Ramona Benton", "Age": 32, "Country": 3, "Address": "Ap #614-689 Vehicula Street", "Married": false }
  ];
  countries = [
    { Name: "", Id: 0 },
    { Name: "United States", Id: 1 },
    { Name: "Canada", Id: 2 },
    { Name: "United Kingdom", Id: 3 }
  ];
  constructor(private elementRef: ElementRef) { }
  ngOnInit() {
    window.$(this.elementRef.nativeElement).jsGrid({
      width: "100%",
      height: "400px",
      inserting: true,
      editing: true,
      sorting: true,
      paging: true,
      data: this.clients,
      fields: [
        { name: "Name", type: "text", width: 150, validate: "required" },
        { name: "Age", type: "number", width: 50 },
        { name: "Address", type: "text", width: 200 },
        { name: "Country", type: "select", items: this.countries, valueField: "Id", textField: "Name" },
        { name: "Married", type: "checkbox", title: "Is Married", sorting: false },
        { type: "control" }
      ]
    })
  }
}
Run Code Online (Sandbox Code Playgroud)
这是 index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Aug4</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
  <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
  <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
错误是:
Date: 2018-08-26T14:01:56.175Z - Hash: fb473e9c8d7de6bf4569 - Time: 224ms
4 unchanged chunks
chunk {main} main.js, main.js.map (main) 52.1 kB [initial] [rendered]
i ?wdm?: Compiled successfully.
ERROR in src/app/structure.directive.ts(26,12): error TS2339: Property '$' does not exist on type 'Window'.
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
这是打字稿类型而不是角度的问题。运行这个命令
npm install @types/jquery --save-dev
Run Code Online (Sandbox Code Playgroud)
或者如果您使用纱线:
yarn add @types/jquery
Run Code Online (Sandbox Code Playgroud)
然后将其添加到类型声明文件中 somefile.d.ts
declare global {
  interface Window {
    $: JQueryStatic
   }
}
Run Code Online (Sandbox Code Playgroud)
这种方法只使类型可用!您仍然需要在某处导入 jquery。
或者,您可以导入 jquery。这将包括库本身和类型。
import * as $ from "jquery"
Run Code Online (Sandbox Code Playgroud)
并在没有窗口前缀的情况下使用它。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           3745 次  |  
        
|   最近记录:  |