有没有办法在Visual Studio Code中设置环境变量?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有没有办法在Visual Studio Code中设置环境变量?相关的知识,希望对你有一定的参考价值。

你可以帮助我,如何在visual studio代码中设置环境变量?

答案

假设您的意思是调试会话(?),那么您可以在env中包含launch configuration属性。

如果在工作区中打开.vscode / launch.json文件或选择“调试”>“打开配置”,则应该会看到一组用于调试代码的启动配置。然后,您可以使用string:string字典向其添加env属性。

以下是从其标准Web模板设置ASPNETCORE_ENVIRONMENTDevelopment的ASP.NET Core应用程序的示例:

{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart",
            "launchBrowser": {
                "enabled": true,
                "args": "${auto-detect-url}",
                "windows": {
                    "command": "cmd.exe",
                    "args": "/C start ${auto-detect-url}"
                },
                "osx": {
                    "command": "open"
                },
                "linux": {
                    "command": "xdg-open"
                }
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}
另一答案

对于更高级的Go语言场景,您可以加载环境文件,如下所示:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch", 
      "mode": "debug",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceFolder}",
      "envFile": "${workspaceFolder}/.env",
      "args": [], 
      "showLog": true
    }
  ]
}

将.env文件放在您的文件夹中,并添加如下所示的变量:

KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'

详细信息:https://medium.com/@reuvenharrison/using-visual-studio-code-to-debug-a-go-program-with-environment-variables-523fea268271

另一答案

如果您已经使用npm模块dotenv分配了变量,那么它们应该显示在您的全局变量中。那个模块是here

运行调试器时,转到“变量”选项卡(如果不可见则右键单击以重新打开),然后打开“全局”,然后“处理”。那么应该有一个env部分......

enter image description here

enter image description here

enter image description here

以上是关于有没有办法在Visual Studio Code中设置环境变量?的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法在实例之间同步我的 Visual Studio Code 设置?

有没有办法在Visual Studio Code中设置环境变量?

有没有办法让 Visual Studio Code 识别 PHP 文件中的 HTML 语法

有没有办法改善 Visual Studio Code 中突出显示的 linting 错误?

有没有办法从 Visual Studio Code 调试 Tomcat Java 应用程序

在 Visual Studio Code 中更改文件的编码