Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Rails中,如何访问服务器端呈现为HTML的React组件 上下文_Javascript_Ruby On Rails_Reactjs_Ruby_Recharts - Fatal编程技术网

Javascript 在Rails中,如何访问服务器端呈现为HTML的React组件 上下文

Javascript 在Rails中,如何访问服务器端呈现为HTML的React组件 上下文,javascript,ruby-on-rails,reactjs,ruby,recharts,Javascript,Ruby On Rails,Reactjs,Ruby,Recharts,我正在尝试将React组件转换为PNG,以便将其附加到Rails邮件程序中的电子邮件中。组件是一个饼 我想在Rails中完成这一切,而不是使用Node.js服务器解决方案,它使用类似于将组件转换为PNG的东西 Rechart将图表组件作为SVG加载,因此我可以访问SVG并使用类似的东西将其转换为PNG 问题 如何在服务器端以HTML形式加载React组件,以便访问SVG进行转换 要在Rails中的服务器端加载组件,我们可以执行以下操作: 在视图中 <%= react_component('

我正在尝试将React组件转换为PNG,以便将其附加到Rails邮件程序中的电子邮件中。组件是一个饼

我想在Rails中完成这一切,而不是使用Node.js服务器解决方案,它使用类似于将组件转换为PNG的东西

Rechart将图表组件作为SVG加载,因此我可以访问SVG并使用类似的东西将其转换为PNG

问题 如何在服务器端以HTML形式加载React组件,以便访问SVG进行转换

要在Rails中的服务器端加载组件,我们可以执行以下操作:

在视图中

<%= react_component('HelloMessage', {name: 'John'}, {prerender: true}) %>
但是,
html\u字符串
输出为:

"<div data-react-class=\"personnel/TrainingStatusChart\" data-react-props=\"{&quot;metaData&quot;:{&quot;currentPage&quot;:1,&quot;scopedCount&quot;:23,&quot;statusGreyCount&quot;:15,&quot;statusHighCount&quot;:5,&quot;statusLowCount&quot;:2,&quot;statusMediumCount&quot;:1,&quot;totalCount&quot;:23,&quot;totalPages&quot;:1,&quot;unscopedCount&quot;:23},&quot;prerender&quot;:true}\"></div>\n"
“\n”
这是反应组件,所有的道具都进入其中。预呈现React组件不会在服务器端将其编译为HTML。因此,不可能在React组件中访问SVG以转换为PNG。具体内容如下:

此预呈现过程无权访问窗口或文档,因此不会加载运行时JavaScript或CSS


我仍然相信这是可能的。也许不知何故使用水豚和无头铬来渲染React组件。但是,我还没有找到这方面的示例实现。

您可以尝试使用控制器的
render\u to\u string
方法:

controller_instance = ActionController::Base.new()  
#or
controller_instance = ApplicationController.new()

html_string = controller_instance.render_to_string template: 'path_to_file'

它接受与渲染相同的参数。更多信息

您可以尝试使用控制器的
渲染到字符串
方法:

controller_instance = ActionController::Base.new()  
#or
controller_instance = ApplicationController.new()

html_string = controller_instance.render_to_string template: 'path_to_file'

它接受与渲染相同的参数。更多信息

它没有呈现整个组件的原因是
react\u component()
实际上没有在服务器上呈现组件(因为它没有执行SSR)
react\u component()
仅在服务器上渲染所需的最少元素,这样它就可以在客户端渲染组件。如果这很明显,我很抱歉,但我只是想确保每个人都在同一页上。它没有呈现整个组件的原因是因为
react\u component()
实际上没有在服务器上呈现组件(因为它没有进行SSR)
react\u component()
仅在服务器上渲染所需的最少元素,这样它就可以在客户端渲染组件。如果这是显而易见的,我会道歉,但我只是想确保每个人都在同一页上。
"<div data-react-class=\"personnel/TrainingStatusChart\" data-react-props=\"{&quot;metaData&quot;:{&quot;currentPage&quot;:1,&quot;scopedCount&quot;:23,&quot;statusGreyCount&quot;:15,&quot;statusHighCount&quot;:5,&quot;statusLowCount&quot;:2,&quot;statusMediumCount&quot;:1,&quot;totalCount&quot;:23,&quot;totalPages&quot;:1,&quot;unscopedCount&quot;:23},&quot;prerender&quot;:true}\"></div>\n"
controller_instance = ActionController::Base.new()  
#or
controller_instance = ApplicationController.new()

html_string = controller_instance.render_to_string template: 'path_to_file'