簡體   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