简体   繁体   English

React Uncaught Reference Error - 未定义对象/类

[英]React Uncaught Reference Error - object/class not defined

I'm still completely new to react. 我还是全新的反应。 I tried creating in index.html file with the following contents: 我尝试使用以下内容在index.html文件中创建:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>React Tutorial</title>
    <!-- Not present in the tutorial. Just for basic styling. -->
    <link rel="stylesheet" href="css/base.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"></script>
  </head>
  <body id="html-body">
    <div id="content"></div>
    <script type="text/babel" src="scripts/box.js"></script>
    <script type="text/babel">
    ReactDOM.render(
      <Box />,
      document.getElementById('content')
    );
    </script>
  </body>
</html>

And in scripts/box.js I have the following code: scripts/box.js我有以下代码:

var Box = React.createClass({
  render: function() {
    return (
        <h2>Purple Monkey</h2>
    );
  }
});

When I view index.html in my browser, I get the error Uncaught ReferenceError: Box is not defined . 当我在浏览器中查看index.html时,我收到错误Uncaught ReferenceError: Box is not defined But when i move the ReactDOM.render line in index.html into the scripts/box.js right after defining the Box class, everything works. 但是当我在定义Box类之后立即将index.html中的ReactDOM.render行移动到scripts/box.js ,一切正常。

Why can't I execute the ReactDOM.render outside of scripts/box.js ? 为什么我ReactDOM.renderscripts/box.js之外执行scripts/box.js How do I properly include/require a react component so it can be used by other javascript files? 如何正确包含/要求反应组件,以便其他javascript文件可以使用它?

我用window.Box =替换了var Box = ,然后一切正常。

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

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