在 Visual Studio Code 中运行 JavaScript [英] Run JavaScript in Visual Studio Code

查看:20
本文介绍了在 Visual Studio Code 中运行 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 Visual Studio Code 执行 JavaScript 并显示结果?

Is there a way to execute JavaScript and display the results using Visual Studio Code?

例如,一个脚本文件包含:

For example, a script file containing:

console.log('hello world');

我认为需要 Node.js,但不知道如何去做?

I assume that Node.js would be needed but can't work out how to do it?

Visual Studio Code 我指的是来自 Microsoft 的新代码编辑器 -不是使用 Visual Studio 编写的代码.

By Visual Studio Code I mean the new Code Editor from Microsoft - Not code written using Visual Studio.

推荐答案

这个方案打算在 node 中运行当前打开的文件并在 VSCode 中显示输出.

我有同样的问题,发现新引入的 tasks 对这个特定用例很有用.这有点麻烦,但这是我所做的:

I had the same question and found newly introduced tasks useful for this specific use case. It is a little hassle, but here is what I did:

在项目的根目录中创建一个 .vscode 目录,并在其中创建一个 tasks.json 文件.将此任务定义添加到文件中:

Create a .vscode directory in the root of you project and create a tasks.json file in it. Add this task definition to the file:

{
    "version": "0.1.0",
    "command": "node",
    "isShellCommand": true,
    "args": [
        "--harmony"
    ],

    "tasks": [
        {
            "taskName": "runFile",
            "suppressTaskName": true,
            "showOutput": "always",
            "problemMatcher": "$jshint",
            "args": ["${file}"]
        }
    ]
}

然后你可以:<代码>按 F1 >输入`运行任务` >输入 >选择`runFile` >输入来运行您的任务,但我发现为打开任务列表添加自定义键绑定更容易.

Then you can: press F1 > type `run task` > enter > select `runFile` > enter to run your task, but I found it easier to add a custom key binding for opening tasks lists.

要添加键绑定,在 VSCode UI 菜单中,转到代码">首选项">键盘快捷键".将此添加到您的键盘快捷键:

To add the key binding, in VSCode UI menu, go 'Code' > 'Preferences' > 'Keyboard Shortcuts'. Add this to your keyboard shortcuts:

{
    "key": "cmd+r",
    "command": "workbench.action.tasks.runTask"
}

当然你可以选择任何你想要的组合键.

Of course you can select whatever you want as key combination.

更新:

假设您正在运行 JavaScript 代码来测试它,您可以通过设置它的 isTestCommand 属性true 然后你可以绑定一个键到 workbench.action.tasks.test 命令 用于单个动作调用.

Assuming you are running the JavaScript code to test it, you could mark your task as a test task by setting its isTestCommand property to true and then you can bind a key to the workbench.action.tasks.test command for a single-action invocation.

换句话说,您的 tasks.json 文件现在将包含:

In other words, your tasks.json file would now contain:

{
    "version": "0.1.0",
    "command": "node",
    "isShellCommand": true,
    "args": [
        "--harmony"
    ],

    "tasks": [
        {
            "taskName": "runFile",
            "isTestCommand": true,
            "suppressTaskName": true,
            "showOutput": "always",
            "problemMatcher": "$jshint",
            "args": ["${file}"]
        }
    ]
}

...并且您的 keybindings.json 文件现在将包含:

...and your keybindings.json file would now contain:

{
    "key": "cmd+r",
    "command": "workbench.action.tasks.test"
}

这篇关于在 Visual Studio Code 中运行 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆