如何使用新的Microsoft Visual Studio code在浏览器中查看我的HTML代码?

使用notepad++,您可以选择在浏览器中运行。我如何用Visual Studio Code做同样的事情?


当前回答

点击左下角的管理图标。单击扩展或快捷键Ctrl+Shift+X

然后搜索扩展与这个关键句打开默认浏览器。你会发现这个扩展。这对我更好。

现在右键单击html文件,你会看到在默认浏览器中打开或快捷键Ctrl+1在浏览器中看到html文件。

其他回答

Live Preview扩展刚刚添加了(在Insiders Build now和稳定2023年2月初)改变打开默认浏览器的能力(当你选择在外部浏览器中打开它,而不是在vscode中的另一个选项卡中打开它)。参见添加选项以选择默认外部浏览器。

背景是

Live Preview: Custom External Browser
livePreview.customExternalBrowser

选项:Edge、Chrome、Firefox、无

没有一个浏览器会使用您在操作系统中设置的默认浏览器。新的设置允许您覆盖默认设置与另一个浏览器的目的Live预览扩展打开外部浏览器,通过命令:

Live Preview: Show Preview (External Browser)

VSCode任务-通过应用程序包标识符打开(仅限macOS)。

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Open In: Firefox DE",
      "type": "process",
      "command": "open",
      "args": ["-b", "org.mozilla.firefoxdeveloperedition", "${file}"],
      "group": "build",
      "problemMatcher": [],
      "presentation": {
        "panel": "shared",
        "focus": false,
        "clear": true,
        "reveal": "never",
      }
    }
  ]
}

在Opera浏览器中打开文件(Windows 64位)。只要加上这几行:

{
"version": "0.1.0",
"command": "opera",
"windows": {
    "command": "///Program Files (x86)/Opera/launcher.exe"
},
"args": ["${file}"] }

注意command:行的路径格式。不要使用“C:\path_to_exe\runme.exe”格式。

要运行此任务,请打开要查看的html文件,按F1,键入task opera并按enter

VS Code团队现在有一个官方扩展,叫做“Live Preview”

快速设置:

安装“实时预览”扩展从微软。 从工作空间打开html文件,当前工作空间之外的文件无法工作。 执行命令> Live Preview: Show Preview(外部浏览器)

还有一个命令用于在内部浏览器中启动它。您可能还需要从扩展设置中更改默认端口,以防它已经在您的系统上使用。

文档:https://marketplace.visualstudio.com/items?itemName=ms-vscode.live-server

发布说明:https://code.visualstudio.com/updates/v1_59#_live-preview

Windows -打开默认浏览器-在VS Code v 1.1.0上测试

回答既打开一个特定的文件(名称是硬编码)或打开任何其他文件。

步骤:

Use ctrl + shift + p (or F1) to open the Command Palette. Type in Tasks: Configure Task or on older versions Configure Task Runner. Selecting it will open the tasks.json file. Delete the script displayed and replace it by the following: { "version": "0.1.0", "command": "explorer", "windows": { "command": "explorer.exe" }, "args": ["test.html"] } Remember to change the "args" section of the tasks.json file to the name of your file. This will always open that specific file when you hit F5. You may also set the this to open whichever file you have open at the time by using ["${file}"] as the value for "args". Note that the $ goes outside the {...}, so ["{$file}"] is incorrect. Save the file. Switch back to your html file (in this example it's "text.html"), and press ctrl + shift + b to view your page in your Web Browser.