简体   繁体   English

将 React 组件集成到现有的 NET Core 应用程序中

[英]Integrate React components into an existing NET Core app

I have an existing application build with.Net Core Framework.我有一个使用 .Net Core Framework 构建的现有应用程序。 I would like to integrate React components for re-usability purposes which at this point will only be app specific.我想集成 React 组件以实现可重用性目的,此时仅针对应用程序。 I have gone through numerous "Hello World!!"我经历过无数次“Hello World!!” tutorials but that doesn't satisfy my need.教程,但这不能满足我的需要。 I have also looked at reactjs.net but that also is not going to help me as the components gets rendered on the View我也看过reactjs.net但这也对我没有帮助,因为组件在视图上呈现

Scenario设想

Application has lots of Modals with a form which gets rendered on numerous pages.应用程序有很多模态框,它们的表单会在许多页面上呈现。 Currently it is being handled with JavaScript.目前正在使用 JavaScript 处理。 The JavaScript code gets duplicated a lot to achieve it.为了实现它,JavaScript 代码被重复了很多次。

Goal目标

Would like to have a react component to replace above mentioned functionality to reduce code getting duplicated.希望有一个反应组件来替换上述功能,以减少代码重复。

The problem I am facing is I am not sure how will I be able to interact with the component from a jQuery/JavaScript point of view.我面临的问题是我不确定如何从 jQuery/JavaScript 的角度与组件进行交互。

Example例子

I have a DataTable and one of the actions is to click on a certain button to display the Modal.我有一个数据表,其中一个操作是单击某个按钮以显示模态。 The code is in a separate .js file so it is separate from the View .该代码位于一个单独的.js文件中,因此它与View是分开的。 So in this case if I click on a button I would like to render the react component.所以在这种情况下,如果我点击一个按钮,我想渲染反应组件。 I would need to pass props through to the components and that is where I am uncertain how would I handle it:-(我需要将道具传递给组件,这就是我不确定如何处理它的地方:-(

Any suggestion or guidance would be appreciated.任何建议或指导将不胜感激。

Thanks in advance提前致谢

Using react in Asp.Net Core application is easy.在 Asp.Net Core 应用程序中使用 react 很容易。 At first you need to know your back-end will be API based to communicate with react.首先,您需要知道您的后端将基于 API 与 React 进行通信。 So if you have not set up your Core application to an API based, it won't work.因此,如果您尚未将您的核心应用程序设置为基于 API,它将无法工作。 Also make sure you have installed nodeJs and other dependencies.还要确保您已经安装了 nodeJs 和其他依赖项。

To get started with react, create a new folder in your Asp.Net project solution.要开始使用 react,请在您的 Asp.Net 项目解决方案中创建一个新文件夹。

Open the folder in your Command line and execute:在命令行中打开文件夹并执行:

npx create-react-app [your--folder--name] npx create-react-app [你的--文件夹--名称]

To view your created app, run要查看您创建的应用程序,请运行

npm start npm启动

To get started with asp.net, you need to add spa dependencies in your project.要开始使用 asp.net,您需要在项目中添加 spa 依赖项。

Then you have to set up your ConfigureService method to include:然后你必须设置你的 ConfigureService 方法来包括:

services.AddSpaStaticFiles(configuration  => { configuration.RootPath  =  "[your--folder--name]/build"; });

Finally set up your Configure method to include:最后设置您的配置方法以包括:

app.UseSpaStaticFiles();    
app.UseRouting();    
app.UseEndpoints( ... );    
app.UseSpa(spa => {        
    spa.Options.SourcePath = "[your--folder--name]";        
    if (env.IsDevelopment()) {            
        spa.UseReactDevelopmentServer(npmScript: "start");
    }    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM