![](/img/trans.png)
[英]VScode editor .xml file extension to show html file inside editor
[英]How to open a local (inside the extension package) file in a vscode extension
我正在創建一個 vscode 擴展。 我想提供一個help
命令,當被調用時,它會打開一個由擴展提供的help.md
文件,因此我假設它是作為擴展 package 的一部分提供的。
myext/
├── .vscode/
├── test/
├── package.json
├── extension.js
├── help.md
如何獲取對擴展 package 內容的引用並在運行擴展的編輯器中打開文件help.md
?
您可以使用擴展上下文獲取擴展中任何文件的路徑。 這是我在擴展中使用的 function 來確定要加載的文件的絕對路徑:
/**
* Returns the absolute path to a file located in our misc folder.
*
* @param file The base file name.
* @param context The context of this extension to get its path regardless where it is installed.
* @param webview When given format the path for use in this webview.
*/
public static getMiscPath(file: string, context: ExtensionContext, webView?: Webview): string {
if (webView) {
let uri = Uri.file(context.asAbsolutePath(path.join('misc', file)));
return webView.asWebviewUri(uri).toString();
}
return context.asAbsolutePath(path.join('misc', file));
}
這個 function 確定了 2 個不同的路徑變體:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.