我是新手,所以请耐心等待。我想做的是让 jquery 在 typescript 代码文件中工作。
我应该如何引用.d.ts代码?
我尝试过以下方法:
import * as $ from "jquery";
这给了我错误:“'jquery'解析为非模块实体,无法使用此构造导入”
然后我尝试了:
import $ = require("jquery");
这会出现以下错误: 以 EcmaScript 2015 模块为目标时无法使用导入分配
我该如何做才能使 jquery 函数在我的 typescript 代码中工作?还有其他方法吗?
我正在使用 typescript 2.5.2、模块系统 ES2015、EcmaScript 6 和 @types/jquery。
编辑
我尝试import * as $ from "../wwwroot/lib/jQuery/dist/jquery.min.js"
;,现在它给我错误“...已解决” ...但是 --allowJs 未设置”,我还没有 tsconfig.json,我可能需要它来执行 --allowJs 命令,对吗?
最佳答案
在文件顶部添加对 Typescript 定义文件的引用。
/// <reference path ="jquery.d.ts"/>
而不是
import * as $ from "jquery";
根据DefinitelyTyped wiki:
A TypeScript declaration file is way of defining the types, functions and parameters in an external third-party JavaScript library. By using a declaration file in your TypeScript code will enable Intellisense and type checking against the external library you are using.
jquery.d.ts 是可在GitHub 上找到的DefinitelyTyped 库的一部分。 。 您也可以通过 npm 安装.
关于jquery - 使用 jquery 和 typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264908/