TypeScript 中的 WebView 扩展

2024-04-12

在代码示例(catcoding)中,支持 webview 逻辑被编写为 JavaScript 中的匿名函数,但我想在 Typescript 中构建此支持逻辑。

我厌倦了用 requireJS 将这个逻辑重现为打字稿包,但我无法让它工作。

// This script will be run within the webview itself
// It cannot access the main VS Code APIs directly.
(function () {
  const vscode = acquireVsCodeApi();

…

}();

我希望在 TypeScript 中构建这个支持 WebView 逻辑,以便获得静态类型检查。


如果您使用 TypeScript 编写 webview 脚本,则必须使用 typescript 编译器或 webpack 将它们编译为 JavaScript(请参阅github 拉取请求扩展 https://github.com/Microsoft/vscode-pull-request-github举个例子)。

VS Code 不包括可用于 Web 视图内的脚本的 VS Code api 的 TypeScript 类型,但您在 TypeScript 中所需要做的就是声明一个名为acquireVsCodeApi exists:

declare var acquireVsCodeApi: any;

const vscode = acquireVsCodeApi();

// Do stuff with api like getting the state
vscode.getState();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TypeScript 中的 WebView 扩展 的相关文章

随机推荐