【问题标题】:How does one get the id from the globalState in a Visual Studio Code extension?如何从 Visual Studio Code 扩展中的 globalState 获取 id?
【发布时间】:2018-09-06 22:41:19
【问题描述】:
在vscode 扩展上下文中,有globalState。 globalState 是一个 ExtensionMemento 对象,在运行时调试时我可以看到它有一个私有的_id。
如何获取id?我试过了:
context.globalState.get<string>("id");
context.globalState.get<string>("_id");
...但每个都返回未定义。
【问题讨论】:
标签:
visual-studio-code
vscode-extensions
【解决方案1】:
激活扩展时,当前扩展上下文作为参数传递。您可以使用context.extensionPath 找到package.json 并解析它。
import * as Path from 'path';
import * as fs from 'fs';
export function activate(context: vscode.ExtensionContext) {
var extensionPath = Path.join(context.extensionPath, "package.json");
var packageFile = JSON.parse(fs.readFileSync(extensionPath, 'utf8'));
if (packageFile) {
var packageId = packageFile.publisher + '.' + packageFile.name;
console.log(packageId);
}
//......... rest
}
【解决方案2】:
context.globalState["_id"]