我正在尝试使用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之间有什么区别?
答案 0 :(得分:1)
通过您提供的doc链接:
ExtensionContext的实例作为扩展的激活调用的第一个参数提供。