Cuthbert's Blog

Visual Studio Code 运行 html 文件右键 Open In Other Browsers 提示找不到 Chrome 的解决办法

Published on
/329 字 · 1 分钟/---

今天想试一下微软的 Visual Studio Code 这款代码编辑器,因为其他的 IDE 虽然功能强大,各种代码提示,但是免不了的原因是体积大,打开会慢,虽然等几秒的时间很短,但是还是受不了。vscode 体积小,打开速度快,更强大的是拓展强大,虽然本身功能少,但是架不住人有个强大的拓展库。

在写了个 html 网页后,用其他浏览器都可以打开网页,但是 chrome 浏览器是个例外,提示 windows 找不到 chrome,试了各种方法都不行。最后找到了解决方法。

因为 chrome 安装时不像其他软件的安装程序一样选择安装路径,等等,而是直接将 Chrome 安装在当前的用户目录。如果你移动了 Chrome 的安装位置,就会出现这个 windows 找不到 Chrome 的问题。

于是我就换了一种思路,通过配置 tasks.json 文件来解决这个问题。

按 ctrl+shift+p 打开命令面板,输入 Configure Task 然后依次操作

然后点击最后一个打开这个文件,进入到了 tasks.json。我们可以看到默认配置如下

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

我们需要对这个代码进行修改,如下

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run HTML file with Chrome",
            "type": "process",
            "command": "chrome",
            "args": ["${file}"],
            "windows": {
                //这里写你电脑的Chrome浏览器的安装位置
                "command": "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

保存后打开 html 文件,按下 Ctrl+Shift+B 就能打开浏览器了。