我正在尝试学习如何创建 vscode 扩展
有一个功能可以将一些文本打印到控制台,但是每次该功能 被调用时,它会创建一个新的输出通道:
const channel = vscode.window.createOutputChannel("debug");
channel.show();
console.log("test");
我怎样才能避免它?我的意思是,仅创建一次频道。
与您想要在 JS/TS 项目中共享的任何其他部分一样,您必须将其导出。在我的 extension.ts 中,我在
activate
函数中创建了输出通道,并提供导出的打印函数来访问它:
import * as vscode from 'vscode';
let outputChannel: vscode.OutputChannel;
export const activate = (context: vscode.ExtensionContext): void => {
outputChannel = vscode.window.createOutputChannel("My Extension");
...
}
/**
* Prints the given content on the output channel.
*
* @param content The content to be printed.
* @param reveal Whether the output channel should be revealed.
*/
export const printChannelOutput = (content: string, reveal = false): void => {
outputChannel.appendLine(content);
if (reveal) {
outputChannel.show(true);
}
};
现在您可以在任何扩展文件中导入
printChannelOutput
并使用要打印的文本调用它。