我知道这是一个老问题,但如果你仍然在将你的 React 应用程序整合到 React.NET 中遇到问题,你应该尝试以下模板并看看这个人是如何做到的。
他使用 webpack 首先构建和编译一组特定于服务器的代码,然后将编译后的 js 拉入 React.NET
app.UseReact(config =>
{
config
.SetLoadBabel(false)
.AddScriptWithoutTransform("~/js/react.server.bundle.js");
});
webpack 配置如下所示。
var path = require('path');
var webpack = require('webpack');
var WebpackNotifierPlugin = require('webpack-notifier');
module.exports = {
entry: {
server: './React/server.jsx',
client: './React/client.jsx',
clientWithRender: './React/clientWithRender.jsx',
},
output: { path: __dirname + '/wwwroot/js/', filename: 'react.[name].bundle.js' },
module: {
loaders: [
{
test: /.jsx?$/,
loader: "babel-loader",
exclude: /node_modules/,
query: {
presets: ['es2015', 'react'],
plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
}
}
]
},
plugins: [
new WebpackNotifierPlugin()
]
};
这是索引
@{
Layout = null;
}
<html>
<head>
<title>Hello React</title>
</head>
<body>
<div id="app">
@{
if (Environment.GetEnvironmentVariables()["ASPNETCORE_ENVIRONMENT"].ToString() != "Development")
{
@Html.React("ReactComponents.App", new { val1 = "Top text" });
}
}
</div>
@{
if (Environment.GetEnvironmentVariables()["ASPNETCORE_ENVIRONMENT"].ToString() != "Development")
{
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script>
<script src="@Url.Content("~/js/react.client.bundle.js")"></script>
@Html.ReactInitJavaScript()
}
else
{
<script src="@Url.Content("~/js/react.clientWithRender.bundle.js")"></script>
}
}
</body>
</html>