我正在尝试在EPiServer项目中使用ReactJS.Net设置服务器端渲染。为此,我使用提供的ReactJS.Net Html扩展进行渲染,但是,当我运行我的网站时
我收到一条错误消息:“ ReactJS.NET尚未正确初始化。请确保已在Startup.cs文件中调用了services.AddReact()和app.UseReact()。”
如果我从尝试使用服务器渲染扩展切换到尝试使用React正常渲染
我收到另一个错误:“ TinyIoCResolutionException:无法解析类型:React.Web.BabelHandler”。
我认为根本问题是,EPiServer项目在启动时如何初始化与普通的.Net MVC项目之间存在一些差异,但是我不确定有什么区别或如何解决。
我正在使用React.Web.Mvc4 v4.0.0 nuget包,Episerver 11和.Net Framework 4.6.2。我采用了我尝试渲染的相同组件,并将其粘贴到没有EPiServer的Vanilla .Net MVC项目中,并且使用扩展名正确渲染了客户端和服务器端,因此该组件不是问题。我发现有人说这个错误通常是在React或ReactDOM没有暴露给全局范围时出现的,因此我对其进行了设置以确保将我的react组件添加到全局范围中,但也没有运气。
这是我的ReactConfig:
public static void Configure()
{
JsEngineSwitcher.Current.DefaultEngineName = V8JsEngine.EngineName;
JsEngineSwitcher.Current.EngineFactories.AddV8();
ReactSiteConfiguration.Configuration
.AddScript("~/Static/js/Components/PrimaryCallout.jsx");
}
这是我的组件:
class PrimaryCallout extends React.Component {
constructor(props) {
super(props);
this.state = {
header: this.props.header,
buttonText: this.props.buttonText,
buttonLink: this.props.buttonLink
};
}
render() {
return (
<div className="primary-callout-container">
<h2 className="primary-callout-header">{this.state.header}</h2>
<a href={this.state.buttonLink} className="primary-callout-link">
<button className="primary-callout-button">{this.state.buttonText}</button>
</a>
</div>
);
}
}
这是我尝试呈现组件的方式:
@Html.React("PrimaryCallout", new
{
header = Model.Header,
buttonText = Model.CalloutLinkText,
buttonLink = Url.ContentUrl(Model.CalloutLink)
})
任何和所有帮助或见解表示赞赏。
谢谢。
经过大量研究后,我确定了我遇到的问题确实与Babel无关,而与React无关。React未初始化错误令人窒息真正的问题。我没有将其范围缩小到Babel设置中的错误,但是我重新进行了捆绑和缩小设置,现在我又恢复运行了。
对于其他任何人,应该从中吸取的教训是,如果您看到此“ ReactJS.NET尚未正确初始化。请确保已在Startup.cs文件中调用了services.AddReact()和app.UseReact()”。错误,然后再加深了解,因为它可能会扼杀您真正问题的错误消息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句