VSCODE API本地文件TypeError:无法读取未定义的属性'extensionPath'

时间:2020-04-21 07:56:22

标签: javascript visual-studio-code webview

我正在尝试使用webview进行VS Code扩展。我想从其他目录(/CSS/index.css)导入样式表。

这是我程序的结构。

markmap-vscode
├─.vscode
├─CSS
│ ├─ index.css
│ └─ mindmap.css
├─examples
├─node_modules
├─src
│ └─ extension.js
...

这是extenstion.js的一部分,当我运行此命令(markmap-vscode.startWebview)时,它将返回TypeError: Cannot read property 'extensionPath。但是其他VS Code API效果很好,我读到vscode.ExtensionContext.extensionPath给出了扩展路径,但是在这段代码中,VS Code找不到ExtensionContext的属性。

  const startWebview = vscode.commands.registerCommand(
    'markmap-vscode.startWebview',
    () => {
      const nowEditor = vscode.window.activeTextEditor;

      const panel = vscode.window.createWebviewPanel(
        'markMap',
        'makrdown to mind map',
        vscode.ViewColumn.Two,
        {
          enableScripts: true,
        },
      );
      // Get path to resource on disk
      try {
        const onDiskPathOfIndex = vscode.Uri.file(
          path.join(vscode.ExtensionContext.extensionPath, 'CSS', 'index.css'),
        );
      } catch (e) {
        console.error(e);
      }

      panel.webview.html = getWebviewContent();

    },
  );

  context.subscriptions.push(startWebview);

它使Webview很好,但无法加载本地文件。为什么找不到extensionPath的API之间有什么区别?

1 个答案:

答案 0 :(得分:1)

通过您提供的doc链接:

ExtensionContext的实例作为扩展的激活调用的第一个参数提供。