首页 > 解决方案 > 如何在“vscode.window.showInformationMessage”中进行点击事件

问题描述

我有一个包含按钮的信息框,所以当我单击按钮时,我需要它在按钮单击事件中导航到网站。

我能够在信息框中添加一个按钮,但现在我对如何在“vscode.window .showInformationMessage”中进行按钮单击事件没有清楚的了解,因为我是打字稿和扩展开发的新手。所以任何人都可以请指导我?

vscode.window
        .showInformationMessage("Click for more Info","Go to Help")
        .then(selection => {
        console.log(selection); 
        });

这会在调试控制台中打印“转到帮助”。这只是我尝试过的。但预期的结果是当我单击“转到帮助”按钮时,它应该导航到特定的 url(例如:https ://www.merriam-webster.com/dictionary/hep )。谁能指导我获得预期的输出,因为我不知道如何获得结果。

标签: typescriptvisual-studio-codevscode-extensions

解决方案


您不需要“点击事件”,只需检查回调中是否已选择该选项:

let GoToHelp = 'Go to Help';
vscode.window.showInformationMessage('Click for more Info', GoToHelp)
    .then(selection => {
      if (selection === GoToHelp) {
        vscode.env.openExternal(vscode.Uri.parse(
            'https://www.merriam-webster.com/dictionary/hep'));
      }
    });

推荐阅读