简体   繁体   English

使用ReactJS.NET的服务器端组件渲染失败

[英]Server-side rendering of component fails using ReactJS.NET

I'm currently trying to get some ReactJS-stuff (from different tutorials) to be rendered on server-side using 'ReactJS.NET'. 我目前正在尝试使用“ ReactJS.NET”在服务器端呈现一些ReactJS的东西(来自不同的教程)。

However, I always get the following message: 但是,我总是收到以下消息:

Could not find a component named 'CommentBox'. 找不到名为“ CommentBox”的组件。 Did you forget to add it to App_Start\\ReactConfig.cs? 您是否忘记将其添加到App_Start \\ ReactConfig.cs?

Looking at the code here it seems to be pretty straight-forward. 这里查看代码似乎很简单。

My ReactConfig.cs: 我的ReactConfig.cs:

public static class ReactConfig
{
    public static void Configure()
    {
        ReactSiteConfiguration.Configuration
            .AddScript("~/Scripts/dist/CommentBox.js");
    }
}

I can't see the point what I am doing wrong here as I thought I simply need to add the (via webpack) generated CommentBox.js to the config and done. 我在这里看不到我在做什么错,因为我以为我只需要(通过webpack)将生成的CommentBox.js到配置中并完成。

In my View I simply try calling @Html.React("CommentBox",new {}) at which point the exception is thrown. 在我的视图中,我只是尝试调用@Html.React("CommentBox",new {}) ,此时将引发异常。

This is the code which can be found in the generated javascript-file: 这是可以在生成的javascript文件中找到的代码:

var CommentBox = (function (_super) {
    __extends(CommentBox, _super);
    function CommentBox() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    CommentBox.prototype.render = function () {
        return __WEBPACK_IMPORTED_MODULE_0_react__["createElement"]("div", { className: "commentBox" },
            __WEBPACK_IMPORTED_MODULE_0_react__["createElement"]("h1", null, "Comment-Box"),
            __WEBPACK_IMPORTED_MODULE_0_react__["createElement"](CommentForm, null),
            __WEBPACK_IMPORTED_MODULE_0_react__["createElement"](CommentList, null));
    };
    return CommentBox;
}(__WEBPACK_IMPORTED_MODULE_0_react__["Component"]));

I think you can try calling it @Html.React("Components.CommentBox",new {}). 我认为您可以尝试将其称为@ Html.React(“ Components.CommentBox”,new {})。 As exposer work this way, i think. 我认为,随着曝光机以这种方式工作。 I am trying same things now and get this error too. 我现在正在尝试相同的操作,并且也收到此错误。 This helps me, but more errors further :) 这对我有帮助,但是更多错误:)

I am using Webpack 2.2.1 and I tackle the problem in this way. 我正在使用Webpack 2.2.1,并且以此方式解决了该问题。 First of all expose the my component(s) inside an object and I end doing this 首先,将我的组件暴露在一个对象中,然后结束此操作

expose const CommentBox;

After that, inside webapck configuration file, I have something like this 之后,在webapck配置文件中,我有这样的内容

{
   entry: '/path/to/CommentBox.js',
   output: {
      library: ['MyComponents'],
      libraryTarget: 'this'
   }
}

In this way, webpack will expose on the global object (you are running V8, so node env), the object MyComponents.CommentBox 这样,webpack将在全局对象(您正在运行V8,因此是节点env)上公开对象MyComponents.CommentBox。

If you're using Webpack with ReactJS.NET, you need to expose the components publicly so ReactJS.NET can access them. 如果您将Webpack与ReactJS.NET一起使用,则需要公开公开这些组件,以便ReactJS.NET可以访问它们。 See the guide here: https://reactjs.net/guides/webpack.html 请参阅此处的指南: https : //reactjs.net/guides/webpack.html

I found that I needed to reference the full namespace path to the component. 我发现我需要引用该组件的完整名称空间路径。 For example, in my case the component name was ResourceAttributesComponent in the Portal.Scheduling namespace, so I had to write @Html.React("Portal.Scheduling.ResourceAttributesComponent", new {}) 例如,在我的情况下,组件名称是ResourceAttributesComponentPortal.Scheduling命名空间,所以我不得不写@Html.React("Portal.Scheduling.ResourceAttributesComponent", new {})

就我而言,我在组件中缺少默认关键字:导出默认函数Test(){...} https://github.com/reactjs/React.NET/issues/835#issuecomment-502616611

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM