繁体   English   中英

React Component无法在ASP.NET MVC应用中呈现

[英]React Component not rendering in ASP.NET MVC app

我目前在我的应用程序中有1个React组件渲染,但是我无法在应用程序中注入任何其他组件。 当我将第二个组件注入html文件时,在控制台中没有出现任何错误。 在这一点上,我不确定我还真正缺少什么? 欢迎任何和所有建议! (注意:我在BundleConfig.cs文件中包含了一个要进行反应的引用)

这是我在其中调用组件的.cshtml文件:

Administration.cshtml

@model List<object>
@using ms.jts.courtspace.web001Web.Models  
@{

ViewBag.Title = "Administration";
}

<h2>Administration</h2>
<div id="admin" 
     data-userType="@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(M‌​odel));">
</div>

@section Scripts{
   <script src="@Url.Content("~/Scripts/moment.min.js")"></script>
   @Scripts.Render("~/bundles/react")
   @Scripts.Render("~/bundles/bootstrap")
   @Scripts.Render("~/bundles/spcontext")
   <script type="text/jsx" src="@Url.Content("../../App/admin.js")"></script>
}


这是我的反应成分,我也在这里使用底漆。

admin.js

import  React, { Component } from "react";
import ReactDOM from "react-dom";
import { TabView, TabPanel } from 'primereact/components/tabview/TabView';

class TabViewDemo extends React.Component {

    render() {
        return (
            <TabView>
                <TabPanel header="Godfather I" leftIcon="fa-calendar">
                    The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughters wedding.
        His beloved son Michael has just come home from the war, but does not intend to become part of his fathers business.
        Through Michaels life the nature of the family business becomes clear. The business of the family is just like the head
        of the family, kind and benevolent to those who give respect,
        but given to ruthless violence whenever anything stands against the good of the family.
    </TabPanel>
                <TabPanel header="Godfather II" rightIcon="fa-print">
                    Francis Ford Coppolas legendary continuation and sequel to his landmark 1972 film, The_Godfather parallels the young
        Vito Corleone's rise with his son Michael's spiritual fall, deepening The_Godfathers depiction of the dark side of
        the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family.
        Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy, killing the local Black Hand
        Fanucci after he demands his customary cut of the tyro's business. With Fanucci gone, Vito's communal stature grows.
    </TabPanel>
                <TabPanel header="Godfather III" leftIcon="fa-bell-o" rightIcon="fa-bookmark-o">
                    After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this
        third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone,
        now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
    </TabPanel>
            </TabView>
        )
    }
}

ReactDOM.render(<TabViewDemo />, document.getElementById("admin"));

的package.json

{
  "version": "1.0.0",
  "name": "jts.court.space",
  "private": true,
  "devDependencies": {
    "webpack": "3.10.0",
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "6.24.1",
    "babel-loader": "7.1.2",
    "babel-core": "^6.26.0"
  },
  "dependencies": {
    "axios": "0.17.1",
    "primereact": "^1.3.0",
    "pusher-js": "4.2.1",
    "react": "16.2.0",
    "react-dom": "16.2.0",
    "react-router-dom": "^4.2.2",
    "react-addons-css-transition-group": "^15.6.2",
    "font-awesome": "^4.7.0",
    "classnames": "^2.2.5",
    "react-burger-menu": "^2.1.11"
  },
  "scripts": {
    "build": "webpack --config webpack.config.js"
  },
  "-vs-binding": {
    "AfterBuild": [
      "build"
    ]
  }
}

webpack.config.js

"use strict";

module.exports = {
    entry: {
        app: "./App/app.js",
        admin: "./App/admin.js"},
  output: {
    filename: "./Scripts/bundle.js"
  },
  module: {
    loaders: [
        {
            test: /\.js$/,
            loader: "babel-loader",
            exclude: /node_modules/,
            query: {
                presets: ["env", "react"]
            }
        },        
    ]
  }
};

解决方案的答案是从根本上在webpack.config.js文件中创建另一个入口点。 然后,在输出中,我必须修改文件名以生成两个单独的bundle.js文件。 这是我更新后的wepback.config.js文件的样子:

"use strict";

module.exports = {
    entry: {
        app: "./App/app.js",
        admin: "./App/admin.js"
    },
  output: {
    filename: "./Scripts/[name]-bundle.js"
  },
  module: {
    loaders: [
        {
            test: /\.js$/,
            loader: "babel-loader",
            exclude: /node_modules/,
            query: {
                presets: ["env", "react"]
            }
        },        
    ]
  }
};

最后,我必须在BundleConfig.cs中添加这两个新的bundle.js文件,如下所示:

bundles.Add(new ScriptBundle("~/bundles/react").Include(
                "~/Scripts/app-bundle.js",
                "~/Scripts/admin-bundle.js"));

我是React的新手,但希望这可以帮助某人。

暂无
暂无

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

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