javascript - VS代码扩展。 'JS Import' 来自 WebView

标签 javascript visual-studio-code vscode-extensions

我正在为带有 webview 的 vscode 作为单个 web 应用程序编写扩展。我的项目具有以下结构: enter image description here

在索引 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*)。 enter image description here

加载地址:

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/

相关文章:

visual-studio-code - VSCode 的多行选项卡

node.js - 使用自定义 Node 脚本作为 shell 创建 Visual Studio Code 集成终端

python - 代码OSS不执行Jupyter notebook

javascript - 打印时调整 div 的大小

c++ - 如何在Windows中的Visual Studio Code上更新includePath?

javascript - 需要帮助设置backbone.js : Uncaught ReferenceError: Backbone is not defined

Firebase服务附加调试器与VS Code

typescript - VsCode 插件 : Color Coding inline SQL in ts file

javascript - npm http包只提供了package.json,没有javascript文件

javascript - 淡入();使用 jQuery 和 CSS3 的盒子元素