简体   繁体   English

vscode 扩展。 来自 webview 的“JS 导入”

[英]vscode extension. 'JS Import' from webview

I'm write extension for vscode with webview as single web application.我正在使用 webview 作为单个 Web 应用程序为 vscode 编写扩展。 My project has the following structure:我的项目具有以下结构: 在此处输入图片说明

In index html I want to import a script.在索引 html 中,我想导入一个脚本。

<script type="module">
import * as CommandHandler from '/core/CommandHadnler.js'
//some code
</script>

WebView setup localResourceRoots 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)
        ]
    } 
 );

I see that there is an attempt to load the module, but I get the error (Ignore RPC* ).我看到有人尝试加载模块,但我收到错误 (Ignore RPC*)。 在此处输入图片说明

load url:加载网址:

vscode-webview://a8f78cdc-7d22-4793-810f-67c2d10dfb67/core/ClientProxy.js vscode-webview://a8f78cdc-7d22-4793-810f-67c2d10dfb67/core/ClientProxy.js

Probably it incorrect Content Security Policy.可能它不正确的内容安全策略。 My policy setup:我的政策设置:

<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'; ">

The path '/core/CommandHadnler.js' is invalid.路径'/core/CommandHadnler.js'无效。 You have to specify the full path of the JavaScript file on the local machine and convert it with Webview.asWebviewUri .您必须在本地机器上指定 JavaScript 文件的完整路径,并将其转换为Webview.asWebviewUri

See https://code.visualstudio.com/api/extension-guides/webview#loading-local-contenthttps://code.visualstudio.com/api/extension-guides/webview#loading-local-content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM