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

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

问题描述

是否可以使用 Visual Studio代码来执行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代码 ,我的意思是Microsoft的新代码编辑器- 不是使用Visual Studio编写的代码.

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

推荐答案

该解决方案旨在在节点中运行当前打开的文件,并在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}"]
        }
    ]
}

然后您可以: press F1 > type `run task` > enter > select `runFile` > enter 来运行您的任务,但是我发现添加自定义键绑定来打开任务列表更加容易.

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代码进行 test 的操作,则可以通过将其任务设置为test 任务.visualstudio.com/docs/editor/tasks_appendix"rel =" noreferrer> isTestCommand属性到true,然后您可以将键绑定到

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天全站免登陆