在 C# 中,我得到一个 JSX 文件作为输入,并想将其转换为 JS。我想使用 babel 库,并使用了这个指南:https://babeljs.io/docs/setup/#installation
但是,我得到了一个异常类型:
An unhandled exception of type 'React.TinyIoC.TinyIoCResolutionException' occurred in React.Core.dll
附加信息:无法解析类型:React.IReactEnvironment
对于这一行:ReactEnvironment.Current
我已经尝试创建一个新的 ASP.NET 项目,没有抛出任何异常并且一切正常。
我的问题是:如何在非 Web 项目中仍然使用 babel?
最佳答案
您需要为非 Web 应用程序运行额外的初始化步骤。 此初始化步骤在 Web 应用程序中自动完成。
如果它是一个控制台应用程序,那么使用这个函数,
private static void Initialize()
{
Initializer.Initialize(registration => registration.AsSingleton());
var container = React.AssemblyRegistration.Container;
// Register some components that are normally provided by the integration library
// (eg. React.AspNet or React.Web.Mvc6)
container.Register<ICache, NullCache>();
container.Register<IFileSystem, SimpleFileSystem>();
}
并在main方法中调用。
查看 React.Net 的 github 控制台示例应用
关于javascript - Babel 将 JSX 翻译成 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39223720/