【问题标题】:External CSS ,JS and Jquery in vscode webview apivscode webview api中的外部CSS,JS和Jquery
【发布时间】:2018-07-10 10:39:48
【问题描述】:

我想在 VSCode WebView API 的 HTML 页面中使用外部 CSS、JS 和 JQuery。

我添加了下面的代码来使用外部 CSS 文件创建 webView,但是我的 HTML 页面在没有 CSS、JS 和 JQuery 更改的情况下加载。我无法了解我们需要如何在 HTML 页面中引用它。

const panel = vscode.window.createWebviewPanel('catCoding', "Cat Coding", vscode.ViewColumn.One, {
    enableScripts: true
});

const onDiskPath = vscode.Uri.file(path.join(__dirname, 'src/webapp/css', 'styles.css'));

const webSrc = onDiskPath.with({ scheme: 'vscode-resource' });

我还在 HTML 中添加了 Content-Security-Policy 的以下代码以消除限制。

<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src vscode-resource: https:; script-src vscode-resource:; style-src vscode-resource:;">

能否请您告诉我如何使用 VSCode Web 视图 API 使用外部 CSS、JS 和 JQuery 呈现 HTML。

【问题讨论】:

    标签: javascript css api webview visual-studio-code


    【解决方案1】:

    也许您需要允许使用 localResourceRoots 访问 src?

    来自cat example,内容位于媒体文件夹中...

    import * as vscode from 'vscode';
    import * as path from 'path';
    
    export function activate(context: vscode.ExtensionContext) {
        context.subscriptions.push(vscode.commands.registerCommand('catCoding.start', () => {
            const panel = vscode.window.createWebviewPanel('catCoding', "Cat Coding", vscode.ViewColumn.One, {
                // Only allow the webview to access resources in our extension's media directory
                localResourceRoots: [
                    vscode.Uri.file(path.join(extensionPath, 'media'))
                ]
            });
    
            const onDiskPath = vscode.Uri.file(path.join(extensionPath, 'media', 'cat.gif'));
            const catGifSrc = onDiskPath.with({ scheme: 'vscode-resource' });
    
            panel.webview.html = getWebviewContent(catGifSrc);
        }));
    }
    
    

    【讨论】:

    • 您好团队,抱歉这是不同的问题并已修复。您能否请我解决以下问题。
    • 我创建了具有三个孩子的树视图。我使用自定义视图容器实现了这一点。有关此更改,请参阅下面的页面。 code.visualstudio.com/updates/v1_23#_extension-authoring 我正在为每个孩子加载 webview。
    • 当我点击第一个子 getChildren 方法时,按照 API 功能调用,然后我加载了 webvew。单击第二个孩子时也会加载相应的webview。问题是,当再次单击第一个孩子时,什么也没有发生(未调用 getChildren 方法)。但我需要为第一个孩子加载相应的 webview。我们需要为此使用任何其他 API 方法吗?
    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多