简体   繁体   English

使用ASP.NET MVC 5应用程序设置ReactJS

[英]Setting up ReactJS with ASP.NET MVC 5 application

I just started learning ReactJS and i am trying to set it up with an ASP.NET MVC application, i just want to create a sample application. 我刚刚开始学习ReactJS,我试图用ASP.NET MVC应用程序进行设置,我只想创建一个示例应用程序。

I installed node version v8.12.0 and npm version 6.10.3 我安装了节点版本v8.12.0和npm版本6.10.3

Created a empty MVC 5 application, with a new folder Jsx inside a folder called Scripts. 创建了一个空的MVC 5应用程序,在名为Scripts的文件夹中带有一个新文件夹Jsx。

In the Jsx folder i have created Tutorial.jsx file. 在Jsx文件夹中,我创建了Tutorial.jsx文件。

var CommentBox = React.createClass({ 
    render: function() { 
        return (
          <div className="commentBox">
            Hello, world! I am a CommentBox.
          </div>
        ); 
    } 
}); 

ReactDOM.render(
    <CommentBox />, 
     document.getElementById('content') 
);

In the Index.Html i have referenced those files. 在Index.Html中,我已经引用了这些文件。

<div id="content"></div>
<script src="@Url.Content("~/Scripts/react/react.js")"></script>
<script src="@Url.Content("~/Scripts/react/react-0.12.2.min.js")"></script>
<script src="@Url.Content("~/Scripts/react/react-dom.js")"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/remarkable/1.7.1/remarkable.min.js"></script>
<script src="@Url.Content("~/Scripts/Jsx/Tutorial.jsx")"></script>

When i am trying to load the Index.html i don't see anything on the page, in the chrome developer tools i see the below error 当我尝试加载Index.html时,我在页面上看不到任何内容,在chrome开发人员工具中,我看到了以下错误

Tutorial.jsx:1 Failed to load resource: the server responded with a status 
of 500 (Internal Server Error)

Not sure exactly why its not able to load the tutorial.jsx. 不知道到底为什么不能加载tutorial.jsx。

Tried replacing <script src="@Url.Content("~/Scripts/Jsx/Tutorial.jsx")"></script> with the <a href="~/Scripts/react/Jsx/Tutorial.jsx"></a> , the error is gone but no output is displayed on screen. 尝试将<script src="@Url.Content("~/Scripts/Jsx/Tutorial.jsx")"></script>替换为<a href="~/Scripts/react/Jsx/Tutorial.jsx"></a> ,错误消失了,但屏幕上没有显示输出。

You cant include your jsx like this in your website. 您不能在网站中包含这样的jsx。 Try to use tool like webpack to transform jsx into js then you can use it. 尝试使用webpack之类的工具将jsx转换为js,然后就可以使用它了。 You can take a look how I setup webpack to transform jsx to js 您可以看看我如何设置webpack来将jsx转换为js

Follow the link here 这里点击链接

Small code snippet bellow. 波纹管小代码段。 I'm using babel-loader to transform jsx and js es6 syntax to browser readable js code 我正在使用babel-loader将jsx和js es6语法转换为浏览器可读的js代码

{
    test: /\.(js|jsx)$/,
    exclude: /node_modules/,
    loader: ["babel-loader", "eslint-loader"]
}

After transform to js file you can include js file to your view 转换为js文件后,您可以将js文件包含在视图中

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

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