【问题标题】:Visual Studio Code: Open current HTML file in browser (launch.json)Visual Studio Code:在浏览器中打开当前 HTML 文件 (launch.json)
【发布时间】:2021-12-22 23:40:24
【问题描述】:

我正在使用 Visual Studio Code,并希望在 Web 浏览器(本例中为 Microsoft Edge)中调试当前打开的 HTML 文件。我的项目文件夹是本地安装的 Web 服务器 (http://localhost) 的 webroot。 我已经安装了 VS Code 扩展“Microsoft Edge Tools for VS Code”并创建了一个包含以下内容的 launch.json 配置:

{
"configurations": [
    {
        "name": "Launch Microsoft Edge and open the Edge DevTools",
        "request": "launch",
        "type": "vscode-edge-devtools.debug",
        "webRoot": "${workspaceFolder}",
        "url": "http://localhost/${relativeFile}"
    }
  ]
}

但是,当我启动此调试器时,Microsoft Edge 打开 URL http://localhost/$%7BrelativeFile%7D,因此变量 ${relativeFile} 似乎未解析。

我该如何解决这个问题并实现我的目标?

【问题讨论】:

    标签: javascript html visual-studio-code vscode-settings


    【解决方案1】:

    relativeFile 将引用当前打开的文件相对于workspaceFolder。需要指定VS Code中打开的文件夹的路径,webroot

    {
    
    "configurations": [
        {
            "name": "Launch Microsoft Edge and open the Edge DevTools",
            "request": "launch",
            "type": "vscode-edge-devtools.debug",
            "webRoot": "${workspaceFolder}"      // "${workspaceFolder}/src"
            "url": "http://localhost/${relativeFile}"
        }
      ]
    }
    

    参考:-https://code.visualstudio.com/docs/editor/variables-reference

    每个 cmets 更新:-

    %7B 和 %7D 是 {} 的 ASCII 码。当你在 launch.json 中时,检查 VScode 的右下角,看看编码是什么?应该是UTF-8

    【讨论】:

    • 我按照建议添加了"webRoot": "${workspaceFolder}", 行,但是Edge 中的URL 仍然是http://localhost/$%7BrelativeFile%7D
    • 我也试过其他的“url”属性变量,比如${file}和${fileBasename},它们也没有解析。
    • 更新了答案。你能试试吗?
    • json 不适用于反引号
    • 哎呀,抱歉没有意识到这一点。您可以检查 VScode 设置中的编码设置吗?
    猜你喜欢
    • 2016-03-16
    • 2020-07-31
    • 1970-01-01
    • 2015-07-14
    • 2016-04-16
    • 1970-01-01
    • 2018-06-09
    相关资源
    最近更新 更多