Visual Studio 2017中的React项目 [英] React project in Visual Studio 2017

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

问题描述

我想与我的.NET应用程序(在同一解决方案中)一起在Visual Studio 2017中开发一个React应用程序.

我正在使用TypeScript,所以我想要一个可以自定义构建的项目类型(我想对项目进行webpack等,因此标准的Visual Studio TypeScript构建是不够的.)

我已经安装了 node.js开发人员工具,但它们只允许我创建特定于node.js的项目(启动时会运行一个node.js实例),不要让我自定义构建过程.

哪种项目类型最适合?

解决方案

我最近做了此操作,并建议以下操作:

  1. 在Visual Studio 2017中创建一个空白"解决方案项目.
  2. 像在Visual Studio中一样,添加/创建.NET项目.
  3. 现在创建您的React项目.我使用来自Facebook的 create-react-app 实际生成了我的React项目.它具有很多非常好的内置功能,例如Webpack,Jest(用于测试),yarn(用于包管理)等.但是,这些细节对您而言是隐藏的",因此生成的项目看起来要简单得多.如果要做需要对构建/测试过程进行更多控制,则可以运行create-react-app eject命令.请注意,这是单向"操作,因为您无法将文件放回create-react-app中.您将需要安装节点 ="noreferrer">纱线.
  4. 由于您还想使用TypeScript,因此应使用 react-scripts- ts 脚本如下:

    create-react-app my-app --scripts-version=react-scripts-ts

    Microsoft在此处进行了很好的介绍.. p>

  5. 棘手的部分是将生成的React项目放入Visual Studio.我是通过从随Visual Studio一起安装的"Visual Studio安装程序"中安装"Node.js开发"模块来完成此操作的.不幸的是,Microsoft似乎已经删除了空白或空的TypeScript项目模板(请参见此处).

  6. 一旦安装了Node.js工具,您就可以在解决方案中创建一个基于节点的项目.在文件->新建->项目...->模板->其他语言-> TypeScript左侧菜单导航下,有几种选择.我选择了空白Node.js Web应用程序".

  7. 之后,您需要将create-react-app创建的React项目文件复制到Visual Studio项目中.我发现在Visual Studio中创建目录比较容易,因此可以将它们添加到项目文件中,然后将文件复制到生成的文件夹中,最后将它们添加到Visual Studio中的项目文件夹中.

  8. 这时,您可以运行create-react-app添加的package.json文件中的脚本.我更喜欢在命令行上运行它们,但是您也可以使用Mads Kristensen的任务运行器资源管理器"在Visual Studio中运行它们.

I want to develop a React application in Visual Studio 2017 alongside my .NET application (in the same solution).

I am using TypeScript, so I want a project type where I can customise the build (I want to webpack the project, etc, so the standard Visual Studio TypeScript build is not enough).

I have installed the node.js developer tools but they only allow me to create node.js specific projects (which run a node.js instance when started) and do not let me customise the build process.

Which project type will be best for this?

解决方案

I recently did this and would recommend the following:

  1. Create a "Blank" solution project in Visual Studio 2017.
  2. Add/create your .NET project as you would normally in Visual Studio.
  3. Now create your React project. I used the create-react-app from Facebook to actually generate my React project. This has a lot of really good built in features such as Webpack, Jest (for testing), yarn (for package management), etc. However, these details are "hidden" from you so the generated project looks much simpler. If you do need more control over the build/testing process you can run the create-react-app eject command. Be advised that this is a "one-way" operation as you can't put the files back in to create-react-app. You will need to install node and yarn separately, if that wasn't obvious.
  4. Since you also want to use TypeScript you should use the react-scripts-ts script like so:

    create-react-app my-app --scripts-version=react-scripts-ts

    Microsoft has a good walk through here.

  5. The tricky part is getting the generated React project into Visual Studio. I did this by installing the "Node.js development" module from the "Visual Studio Installer" that gets installed with Visual Studio. Unfortunately, Microsoft seems to have removed the blank or empty TypeScript project template (see here).

  6. Once the Node.js tools are installed you can create a node based project in your solution. There are several to choose from under the File -> New -> Project... -> Templates -> Other Languages -> TypeScript left menu navigation. I chose "Blank Node.js Web Application".

  7. After that you will need to copy over the React project files created by create-react-app into your Visual Studio project. I find it easier to create the directories in Visual Studio so they are added to the project file, then copy the files the generated folders, and finally add them to the project folder in Visual Studio.

  8. At this point you can run the scripts in the package.json file that were added by create-react-app. I prefer to run these at the command line, but you can run them in Visual Studio as well using the "Task Runner Explorer" by Mads Kristensen.

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

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