Visual Studio 2017 中的反应项目

时间:2023-02-24
本文介绍了Visual Studio 2017 中的反应项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在 Visual Studio 2017 中与我的 .NET 应用程序一起开发一个 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-appject 命令.请注意,这是一个单向"操作,因为您无法将文件放回 create-react-app.您需要安装 node 和 纱线,如果不明显的话.
  4. 既然你也想使用 TypeScript,你应该使用 react-scripts-ts 脚本如下:

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

    微软在这里有很好的了解..p>

  5. 棘手的部分是将生成的 React 项目导入 Visual Studio.我通过从随 Visual Studio 一起安装的Visual Studio 安装程序"安装Node.js 开发"模块来做到这一点.不幸的是,微软似乎已经删除了空白或空的 TypeScript 项目模板(请参阅此处).

  6. 安装 Node.js 工具后,您可以在解决方案中创建基于节点的项目.在 File -> New -> Project... -> Templates -> Other Languages -> TypeScript 左侧菜单导航下有几个可供选择.我选择了空白 Node.js Web 应用程序".

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

  8. 此时,您可以运行 create-react-app 添加的 package.json 文件中的脚本.我更喜欢在命令行中运行它们,但您也可以在 Visual Studio 中运行它们,也可以使用 Mads Kristensen 的Task Runner Explorer".

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 中的反应项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何防止 Visual Studio 2017 构建 JavaScript? 下一篇:Jasmine 2.0:重构 1.3 的 runs() 和 waitsFor()

相关文章

最新文章