我正在为带有 webview 的 vscode 作为单个 web 应用程序编写扩展。我的项目具有以下结构:
在索引 html 中,我想导入一个脚本。
<script type="module">
import * as CommandHandler from '/core/CommandHadnler.js'
//some code
</script>
WebView 设置localResourceRoots
const UI_PATH = path.join(context.extensionPath, 'UI');
this._panel = vscode.window.createWebviewPanel(
'JSONRPCTester',
'JSONRPC Tester',
vscode.ViewColumn.Two,
{
enableScripts: true,
localResourceRoots: [
vscode.Uri.file(UI_PATH)
]
}
);
加载地址:
vscode-webview://a8f78cdc-7d22-4793-810f-67c2d10dfb67/core/ClientProxy.js
可能是内容安全策略不正确。我的策略设置:
<meta http-equiv="Content-Security-Policy" content="default-src self; img-src vscode-webview-resource:; script-src vscode-webview-resource: 'self' 'unsafe-inline'; style-src vscode-webview-resource: 'self' 'unsafe-inline'; ">
最佳答案
路径 '/core/CommandHadnler.js'
无效。您必须在本地计算机上指定 JavaScript 文件的完整路径,并使用 Webview.asWebviewUri
进行转换。
参见 https://code.visualstudio.com/api/extension-guides/webview#loading-local-content
关于javascript - VS代码扩展。 'JS Import' 来自 WebView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63924805/