VSCODE API local file TypeError: Cannot read property 'extensionPath' of undefined

VSCODE API local file TypeError: Cannot read property 'extensionPath' of undefined

我正在尝试使用 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)时,它returnsTypeError: Cannot read property 'extensionPath。但是其他 VS Code API 运行良好,我读到 vscode.ExtensionContext.extensionPath 给出了扩展路径,但是在这段代码中,VS Code 找不到 属性 of 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不错,就是不能加载本地文件。 API 与为什么找不到 extensionPath 之间有什么区别?

根据您提供的文档 link:

An instance of an ExtensionContext is provided as the first parameter to the activate-call of an extension.