如果您正在使用 Visual Studio Code 的调试模式,则可以添加以下选项:
{
"outputCapture": "std"
}
这将把您的日志重定向到调试控制台。
编辑:正如Luigi04在评论部分提到的那样,此设置需要放置在launch.json中。
在 launch.json
中(使用 F1 打开),在 configurations
下,添加 outputCapture
属性(如果还不存在),然后将其设置为 std
或 console
,如下所示:
{
...
"configurations": [
{
...
"outputCapture": "std", // or "console"
}
]
}
"std"
指的是以下文档中的说明:
outputCapture
- 如果设置为 std,则进程 stdout/stderr 的输出将显示在调试控制台中,而不是通过调试端口监听输出。这对于直接写入stdout/stderr流的程序或日志库而非使用console.*
API 的程序很有用。
还要注意,使用 std
将显示完整的错误信息(适用于VSCode 1.49.0)。例如,创建一个包含错误的JS文件:
console.log(a) // error: a is undefined
使用std
:
c: \Users\path\to\file.js: 1
console.log(a) // error: a is undefined
^
ReferenceError: a is not defined
at Object.<anonymous>(c: \Users\path\to\file.js: 1: 13)
at Module._compile(internal / modules / cjs / loader.js: 1158: 30)
at Object.Module._extensions..js(internal / modules / cjs / loader.js: 1178: 10)
at Module.load(internal / modules / cjs / loader.js: 1002: 32)
at Function.Module._load(internal / modules / cjs / loader.js: 901: 14)
at Function.executeUserEntryPoint[as runMain](internal / modules / run_main.js: 74: 12)
at internal / main / run_main_module.js: 18: 47
使用console
:
Uncaught ReferenceError: a is not defined
在我看来,std
稍微好一些。
{"version":"0.2.0","configurations":[{"type":"node","request":"launch","name":"Launch Program","skipFiles":["<node_internals>/**"],"program":"${workspaceFolder}/src/app/pages/auth/auth.component.ts","outFiles":["${workspaceFolder}/**/*.js"],"console":"internalConsole"}]}
如果您正在浏览器中运行和测试代码,则按“F12”(对于Google Chrome)以在浏览器中查看日志。 如果您正在调试模式下运行Visual Studio Code,则仅会显示日志。在菜单栏中有调试选项可在调试模式下运行,或者您可以在此处找到完整的参考资料。