我正在嘗試訪問context.globalState.get('data');TreeView refresh() 函式。我的refresh()樣子是這樣的:
refresh(offset?: number): void {
this.ctmInfrastructureCache = getGlobalStateData();
this.parseTree();
if (offset) {
this._onDidChangeTreeData.fire(offset);
} else {
this._onDidChangeTreeData.fire(undefined);
}
}
全域狀態資料應該由以下人員提供:
export function getGlobalStateData(): string {
const data: string = context.globalState.get('ctmInfrastructureCache');
return data;
}
但是,我無法訪問 context.globalState。我越來越:
型別“SuiteFunction”.ts(2339) 上不存在屬性“globalState”
在呼叫 refresh 并將新資料分配給 globalState 背景關系之前收集新資料。我想要的只是獲取資料并將其作為新 TreeView 的輸入。
我試過添加context: vscode.ExtensionContext到refresh()無濟于事。如何訪問globalState重繪 內部?
類 constructor() 可以訪問 globalState。這是有效的:
constructor(context: vscode.ExtensionContext) {
let ctmInfrastructureCacheTmp: any = context.globalState.get('ctmInfrastructureCache');
let ctmInfrastructureCacheType: any = typeof ctmInfrastructureCacheTmp;
// check if json needs to be converted
if (ctmInfrastructureCacheType === "string") {
this.ctmInfrastructureCache = ctmInfrastructureCacheTmp;
} else {
this.ctmInfrastructureCache = JSON.stringify(ctmInfrastructureCacheTmp);
}
this.parseTree();
}
uj5u.com熱心網友回復:
您缺少的是背景關系被傳遞給建構式,因此它可以在那里訪問它。在你的getGlobalStateData()功能context是未知的。對的參考context可能參考了不同的背景關系物件。
一種可能的解決方案是保留對類的參考context并將其用作類的成員:
public constructor(private context: vscode.ExtensionContext) {
let ctmInfrastructureCacheTmp: any = context.globalState.get('ctmInfrastructureCache');
let ctmInfrastructureCacheType: any = typeof ctmInfrastructureCacheTmp;
// check if json needs to be converted
if (ctmInfrastructureCacheType === "string") {
this.ctmInfrastructureCache = ctmInfrastructureCacheTmp;
} else {
this.ctmInfrastructureCache = JSON.stringify(ctmInfrastructureCacheTmp);
}
this.parseTree();
}
public refresh(offset?: number): void {
this.ctmInfrastructureCache = this.context.globalState.get('ctmInfrastructureCache');
this.parseTree();
if (offset) {
this._onDidChangeTreeData.fire(offset);
} else {
this._onDidChangeTreeData.fire(undefined);
}
}
uj5u.com熱心網友回復:
根據 Mike 的建議,我更新了代碼,現在可以使用了。首先,我將“背景關系”添加到我的 refresh() 的 registerCommand() 部分。
ctmInfrastructureProvider.refresh(undefined, context);
然后我像這樣更新了重繪 函式,將 'context?: vscode.ExtensionContext' 作為可選輸入。
public refresh(offset?: number, context?: vscode.ExtensionContext): void {
// get globalState data
let ctmInfrastructureCacheTmp: any = context.globalState.get('ctmInfrastructureCache');
let ctmInfrastructureCacheType: any = typeof ctmInfrastructureCacheTmp;
// check if json needs to be converted
if (ctmInfrastructureCacheType === "string") {
this.ctmInfrastructureCache = ctmInfrastructureCacheTmp;
} else {
this.ctmInfrastructureCache = JSON.stringify(ctmInfrastructureCacheTmp);
}
this.parseTree();
if (offset) {
this._onDidChangeTreeData.fire(offset);
} else {
this._onDidChangeTreeData.fire(undefined);
}
}
問候,O。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/480861.html
