我如何在React-Rails中使用这个React库 [英] How can I use this React library with React-Rails
问题描述
我想到了这一点:
https://github.com/ssorallen/turbo-react
我喜欢它的功能,对于在当前使用反应栏宝石
And I like what it does, I am just a bit confused about how I use this code within my Rails project that currently uses the React-Rails gem
我对于将代码放置在何处感到很困惑,以便可以使用React Rails将其读取到我的Rails gem中.如果我将主JS文件粘贴到我的vendor/assets文件夹中,则会收到以下消息:
I am mostly confused about where to put the code so that I can read it into my Rails gem with React Rails. If I paste the main JS file into my vendor/assets folder, I get this message:
Uncaught ReferenceError: global is not defined
我知道我只是从根本上误解了如何将这种JS文件包含到我的Rails项目中.
I know that I am just fundamentally misunderstanding how to include this kind of JS file into my Rails project.
推荐答案
public/dist/turbo-react.min.js ,已被编译"为在浏览器中运行(使用Webpack).您应该将该文件复制到vendor/assets
,然后在application.js
中要求.
src/turbo-react.js is meant for a Node.js environment, not a browser environment. I see another file, public/dist/turbo-react.min.js, which has been "compiled" to run in a browser (using Webpack). You should copy that file into vendor/assets
, then require it in application.js
.
src/turbo-react.js
的前几行给了我一些信号,表明它适用于Node.js:
The first few lines of src/turbo-react.js
gave me some signals that it is for Node.js:
if (global.Turbolinks === undefined) {
throw "Missing Turbolinks dependency. TurboReact requires Turbolinks be included before it.";
}
var HTMLtoJSX = require("htmltojsx");
var JSXTransformer = require("react-tools");
var React = require("react");
global
和require
都是浏览器中不存在的Node.js概念.
global
and require
are both Node.js concepts which don't exist in the browser.
这篇关于我如何在React-Rails中使用这个React库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!