VSCode扩展webview外部html和css

VSCode扩展webview外部html和css,html,css,visual-studio-code,vscode-extensions,Html,Css,Visual Studio Code,Vscode Extensions,我正在尝试创建vscode扩展,目标是显示带有外部html和css的webview。 加载和设置html是可行的,但不幸的是css无法加载 创建webview、加载html和设置: var resultPanel = vscode.window.createWebviewPanel("test", "TestWebView", vscode.ViewColumn.One, {}); fs.readFile(path.join(context.extensionPath,'src','la

我正在尝试创建vscode扩展,目标是显示带有外部html和css的webview。
加载和设置html是可行的,但不幸的是css无法加载

创建webview、加载html和设置:

var resultPanel = vscode.window.createWebviewPanel("test", "TestWebView", vscode.ViewColumn.One, {});
    fs.readFile(path.join(context.extensionPath,'src','languageMenue.html'),(err,data) => {
          if(err) {console.error(err)}
          resultPanel.webview.html = data;
      });
这是可行的,在html中,css的加载方式如下:

<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="theme.css">

css与html位于同一文件夹中(在扩展项目的src文件夹中)


我错过了什么?为什么css无法加载?

请通读vscode webview指南:

特别注意“加载本地内容”部分:

出于安全原因,对于如何加载外部内容有一些特殊的规则

请看这个

关于
localResourceRoots
的注意事项如下:

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(context.extensionPath, 'media'))]
        }
      );
您可以将该行编辑为:

localResourceRoots:[vscode.Uri.file(path.join(context.extensionPath,'your/relative/location/here/in/your/extension/path'))
要实现此功能,必须指定存储
css
文件的绝对路径

i、 e

其中,
{{styleSrc}}
绑定为:

const styleSrc = vscode.Uri.file(path.join(vscode.context.extensionPath, '/path/to/dir/of/theme.css')).with({ scheme: 'vscode-resource' })
或者使用
asWebviewUri
API作为:

const onDiskPath = vscode.Uri.file(path.join(vscode.context.extensionPath, '/path/to/dir/of/theme.css'))
const styleSrc = panel.webview.asWebviewUri(onDiskPath)
其中
面板
是使用
vscode.window.createWebviewPanel(…)创建的面板


有关更多信息,请参见:

与我相同,不作为简单CSS链接使用。你找到实现这个的方法了吗?const-onDiskPath=vscode.Uri.file(path.join(vscode.context.extensionPath,'/path/to/dir/of/theme.css'))const-styleSrc=panel.webview.asWebviewUri(onDiskPath)这对我很有效
const styleSrc = vscode.Uri.file(path.join(vscode.context.extensionPath, '/path/to/dir/of/theme.css')).with({ scheme: 'vscode-resource' })
const onDiskPath = vscode.Uri.file(path.join(vscode.context.extensionPath, '/path/to/dir/of/theme.css'))
const styleSrc = panel.webview.asWebviewUri(onDiskPath)