Visual Studio 2019 - 添加客户端库(TypeScript,JQuery等)的正确方法



[2019年7月16日编辑#1] 我很困惑。 我正在试验一个.NET Core 3.x Web应用程序,我想在其中使用:

  • jQuery
  • 打字稿

我有TypeScript工作,但它拒绝理解jQuery"$"符号。

我通过NuGet添加了"DefinitelyTyped"库,但没有乐趣。

我认为我的困惑在于我应该如何添加这些客户端库。 我已经尝试通过添加客户端库(通过右键单击解决方案)来尝试,这在libman.json文件中。 还有一种方法可以使用package.json(我认为是node)。

我真正想了解的是Visual Studio 2019的首选方式是什么。

关于具体问题(我真的可以通过一些"新手"指导来完成)....

在"依赖项"下,我现在有:

NPN

  • 戴尔
  • 吞掉
  • jquery
  • jquery-ui

NuGet

  • jquery.TypeScript.DefinitelyTyped
  • jqueryui.TypeScript.DefinitelyTyped Microsoft
  • Microsoft.TypeScript.MSBuild

然后,在"脚本"下,我有例子.ts

var example = {
showGreeting: function(): void {
alert("Hello user");
let x: any = $('abc');
}
};
example.showGreeting();

(这是无法识别"$"符号的地方)

tsconfig.json

{
"compilerOptions": {
"noImplicitAny": true,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"files": [
"./example.ts"
],
"compileOnSave": true
}

libman.json

{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"provider": "unpkg",
"library": "bootstrap@4.3.1",
"destination": "wwwroot/lib/bootstrap/"
}
]
}

包.json

{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"dependencies": {
"jquery": "3.4.1",
"jquery-ui": "1.12.1"
},
"devDependencies": {
"gulp": "4.0.2",
"del": "5.0.0"
}
}

编辑 #1

我从package.json的Dependencies部分删除了客户端库,并将它们移动到libman.json的库部分。 它们从解决方案的"npm"依赖项下适当地消失了,现在出现在 wwwroot/lib 下。

libman.json

{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"provider": "unpkg",
"library": "bootstrap@4.3.1",
"destination": "wwwroot/lib/bootstrap/"
},
{
"library": "jquery@3.4.1",
"destination": "wwwroot/lib/jquery/"
},
{
"library": "jqueryui@1.12.1",
"destination": "wwwroot/lib/jqueryui/"
}
]
}

包.json

{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"dependencies": {
},
"devDependencies": {
"gulp": "4.0.2",
"del": "5.0.0"
}
}

我认为这是朝着正确方向迈出的一步。

但是,TypeScript 仍然给我错误:

错误 TS2581:内部版本:找不到名称"$"。是否需要安装类型 jQuery的定义?

好的 - 现在开始工作了。 下面定义了我必须安装的内容。

NuGet

  • Microsoft.TypeScript.MSBuild
  • Microsoft.Web.LibraryManager.Build

Libman.json

{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"provider": "unpkg",
"library": "bootstrap@4.3.1",
"destination": "wwwroot/lib/bootstrap/"
},
{
"library": "jquery@3.4.1",
"destination": "wwwroot/lib/jquery/",
"files": [
"jquery.min.js",
"jquery.min.map",
"jquery.js",
"core.js"
]
}
]
}

包.json

{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"dependencies": {
},
"devDependencies": {
"gulp": "4.0.2",
"del": "5.0.0",
"@types/jquery": "3.3.30",
"@types/jqueryui": "1.12.7"
}
}

最新更新