Angular Cli 全局 jquery: TS2304: 找不到名称“$”

Angular Cli global jquery: TS2304: Cannot find name '$'

全局使用 jquery 和 jquery 插件时。 ng 编译器给出以下错误:

TS2304: Cannot find name '$'.

vscode 成功识别了 tsconfig 中定义的全局 typings。虽然编译器抛出错误,但它仍然成功地全局加载插件。是否可以抑制此错误?

解决方案一: import * as $ from 'jquery'

这不是一个可行的解决方案,因为 jquery 插件中的 typingsvscode 中不再可用。

配置

tsconfig:

"typeRoots": [
      "node_modules/@types" // where jquery is also installed
    ],

angular-cli:

"scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/datatables.net/js/jquery.dataTables.js",
    "../node_modules/datatables.net-bs/js/dataTables.bootstrap.js"
  ],

事实证明 typeRoots 还不够。只需直接为 DataTables 添加 types