例如在 html 文件中我有
<script src="https://d3js.org/d3.v6.min.js"></script>
,我想获得诸如 d3->csv
等提示。
在 WebStorm 中,可以通过将
https://d3js.org/d3.v6.min.js
添加到 External Libraries
来实现。有没有办法在 VSCode 中做到这一点?
它是一个简单的 HTML+JS 文件,没有 npm、node 等,例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
d3.SHOW_HINTS
</script>
</body>
</html>
对于您的
d3
用例,您需要做的就是:
<script>
标签内容从 HTML 文件移动到独立的 JS 文件中。npm install @types/d3
,就像 Mike Lischke 已经指出的那样结果:
您无法在 HTML 文件内的
<script>
标记中获得相同类型的 IntelliSense 提示,因为官方扩展不支持它。请参考我对另一个问题的回答这里了解更多详情。
您需要的是智能感知信息,这是由 JS/TS 语言实现提供的。除了实际的 TS 代码(如果提供)之外,它还使用所谓的“打字(或声明)文件”。许多不同的库都有类型。对于 D3.js,请使用 npm install @types/d3
或将
"@types/d3": "^5.7.2"
添加到 package.json 中的开发依赖项部分,然后运行 npm install
。<script type="module" src="script.js"></script>
并且在
script.js
文件的顶部包含这样一行:
import "https://d3js.org/d3.v5.min.js";