从 Visual Studio 2019 创建默认项目后:
After creating the default project from Visual Studio 2019:
ASP.NET Core Web 应用程序
ASP.NET Core Web Application
然后使用 React 默认模板(The Weather Forecast one)创建一个项目,我立即运行它,没有修改任何东西.
And then using the React default template(The Weather Forecast one) to create a project, I run it immediately without modifying anything.
它构建时没有错误,但在浏览器中运行并打开选项卡时,我收到一个错误:
It builds with no errors, but upon running and opening a tab in the browser, I get an error:
compiler.hooks.done.tap('done', async stats => { ^^^^^ SyntaxError: missing ) after argument list at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:542:28) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19)我什至尝试从 NuGet 包安装最新版本的 npm 和 react.core,但没有任何改变.每次运行都出现同样的错误.
I even tried installing the latest versions of npm and react.core from NuGet packages, but nothing changed. Same error every time I run it.
推荐答案我经常使用模板,包括 React.您可以尝试以下两件事.
I work with the templates a lot, and including React. Here are two things you can try.
- 模板一直在修复和更新,尤其是那些依赖于 React 等外部框架的模板.例如,我记得几个月前 React 没有在一个全新的项目上工作.我记得,在另一个新模板创建的项目中,即使引导程序也没有开箱即用.为了确保在看到您的问题后,我在 Visual Studio 中创建了一个新的 React 应用程序并且运行良好.
因此,解决方案 #1 - 更新您的 Visual Studio 安装.我很确定您使用的是过时版本的模板.
So, solution # 1 - Update your Visual Studio installation. I am pretty sure you are using an outdated version of template.
- 好的,现在,如果您仍然遇到错误怎么办.或者,我所做的,回到模板损坏时.简单的.请记住,模板旨在帮助您,但它并不是实际使用您想要的框架的方式.请注意,您可以随时将 JS 框架添加到您的 dot net core.
所以,解决方案#2
就您而言,react 是单页应用程序.此处讨论 - UseStaticFiles、UseSpaStaticFiles、和 ASP.NET Core 2.1 中的 UseSpa?
In your case, react is a Single Page Application. discussion here - What is the difference between UseStaticFiles, UseSpaStaticFiles, and UseSpa in ASP.NET Core 2.1?
另一个发生类似讨论的地方是这里 - www.roundthecode/dotnet/add-a-react-app-to-your-asp-net-core-application
another place where a similar discussion is happening is here - www.roundthecode/dotnet/add-a-react-app-to-your-asp-net-core-application
本质上,您为 React SPA 创建一个文件夹并执行类似操作.这里的ClientApp"是您添加 React 项目的文件夹.
Essentially, you create a folder for your React SPA and do something like this. Here 'ClientApp' is the folder where you add your react project.
app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseReactDevelopmentServer(npmScript: "start"); } });更多推荐
Visual Studio 2019 中的默认 ASP.NET Core + React 模板不起作用
发布评论