如何解决vscode扩展从Webview'JS导入'
我正在使用Webview作为单个Web应用程序编写vscode扩展。我的项目具有以下结构:
在索引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)
]
}
);
我看到有尝试加载模块的尝试,但是出现错误(忽略RPC *)。
加载网址:
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。