我一直在使用 ReactJs.Net 和 MVC5 应用程序,并在使用 ES6 和 babel 转译器时遇到服务器端渲染问题。我使用了expose-loader 来全局公开组件。但它只适用于 ES5 语法。如果我使用 ES6 和 Webpack 进行捆绑,我会在 @Html.React() 函数中收到以下错误。
将“Hello”渲染为“react_Hq6cd5QfaUu26XrhGgZA”时出错:脚本抛出异常:Minified React 错误 #130;访问http://facebook.github.io/react/docs/error-decoder.html?
~Jsx/Components/Helloworld.jsx 的代码
import React, {Component} from 'react';
export default class Helloworld extends Component {
render() {
return (
<div>Hello {this.props.name} ,This is my first react component.</div>
);
}
}
Code for ~Jsx/server.jsx
import Hello from 'expose?Hello!./components/Helloworld' ;
webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports =
{
entry: ['./Jsx/server.jsx'],
output: { path: './build', filename: 'main.js' },
module: {
loaders: [
{
test: /\.jsx$/,
loader: ['babel-loader'],
include: path.resolve(__dirname, 'Jsx'),
query: {
presets: ['es2015', 'react']
}
},
]
},
resolve: {
extensions: ['', '.js', '.jsx']
}
//watch:true
}
RectConfig.cs
ReactSiteConfiguration.Configuration
.SetLoadBabel(false)
.AddScriptWithoutTransform("~/build/main.js");
The above code works fine if i use the below ES5 syntax for Helloworld component
var React=require('react');
var Helloworld = React.createClass({
render: function() {
return (
<div>
Hello {this.props.name} ,This is my first react component.
</div>
);
}
});
module.exports=Helloworld;
如果有人知道解决方案,请帮助我。