Javascript 在Rails中,如何访问服务器端呈现为HTML的React组件 上下文
我正在尝试将React组件转换为PNG,以便将其附加到Rails邮件程序中的电子邮件中。组件是一个饼 我想在Rails中完成这一切,而不是使用Node.js服务器解决方案,它使用类似于将组件转换为PNG的东西 Rechart将图表组件作为SVG加载,因此我可以访问SVG并使用类似的东西将其转换为PNG 问题 如何在服务器端以HTML形式加载React组件,以便访问SVG进行转换 要在Rails中的服务器端加载组件,我们可以执行以下操作: 在视图中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_component('HelloMessage', {name: 'John'}, {prerender: true}) %>
但是,html\u字符串
输出为:
"<div data-react-class=\"personnel/TrainingStatusChart\" data-react-props=\"{"metaData":{"currentPage":1,"scopedCount":23,"statusGreyCount":15,"statusHighCount":5,"statusLowCount":2,"statusMediumCount":1,"totalCount":23,"totalPages":1,"unscopedCount":23},"prerender":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=\"{"metaData":{"currentPage":1,"scopedCount":23,"statusGreyCount":15,"statusHighCount":5,"statusLowCount":2,"statusMediumCount":1,"totalCount":23,"totalPages":1,"unscopedCount":23},"prerender":true}\"></div>\n"
controller_instance = ActionController::Base.new()
#or
controller_instance = ApplicationController.new()
html_string = controller_instance.render_to_string template: 'path_to_file'