Reactjs React.js输出到jsc:ReferenceError:<;组件>;没有定义

Reactjs React.js输出到jsc:ReferenceError:<;组件>;没有定义,reactjs,Reactjs,我正在rails项目中使用react railsgem。虽然javascript和jsx工作一致,但我的coffeescript文件似乎从未工作过。请注意,我尝试使用纯coffeescript解决方案,没有插入jsx 在我的coffeescript文件中(其扩展名是*.js.coffee) 我认为: = react_component 'Hello', name: 'World' 这是我在控制台中经常遇到的错误: ReferenceError: Hello is not defined ja

我正在rails项目中使用
react rails
gem。虽然
javascript
jsx
工作一致,但我的
coffeescript
文件似乎从未工作过。请注意,我尝试使用纯
coffeescript
解决方案,没有插入jsx

在我的
coffeescript
文件中(其扩展名是
*.js.coffee

我认为:

= react_component 'Hello', name: 'World'
这是我在控制台中经常遇到的错误:

ReferenceError: Hello is not defined
jakubmal在《react rails》repo》中从我的GitHub中获取答案:

CoffeeScript创建了一个闭包,看起来很像:

(function() {
  var div, hello;

  div = React.DOM.div;

  Hello = React.createClass({
    render: function() {
      return div({}, ['Hello ' + this.props.name]);
    }
  });
}).call(this);
使
Hello
在闭包外无法访问

您可以将
Hello
分配给如下窗口:

window.Hello = React.createClass
或者使用快捷方式/技巧:

@Hello = React.createClass
要保持应用程序结构整洁,您需要至少应用 这里是名称空间模式。

此外,Paul O'Shannessy在google小组中写道:

助手非常幼稚,希望您的组件可以作为全局组件使用。Coffeescript将每个文件包装在一个闭包中,然后再由链轮连接,这违反了全局假设。这是在开发过程中提出的,但我们决定,对某些人来说,做点什么总比什么都不做要好

@Hello = React.createClass