CDN在做出反应,我怎样才能学会如何恰当地使用它们?

时间:2017-12-14 15:08:09

标签: javascript reactjs cdn jsx

为什么我使用:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>

它的作品,但当我使用官方网站上的那些:

<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>

我正在使用课程:

class App extends React.Component{

和ReactDOM;

ReactDOM.render(<App />, document.querySelector("#root"));

为什么会这样说&#34; React&#34;不见了?

CDN在做出反应,我怎样才能学会如何恰当地使用它们?

1 个答案:

答案 0 :(得分:3)

cdnunpkg都可以使用。这是工作示例,

<!DOCTYPE html>
<html>
  <head>
    <title>React Hello World</title>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.js"></script>
    <script crossorigin src="https://unpkg.com/react@16.0.0/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script type="text/jsx">
      class App extends React.Component {
          render() {
              return (
                <div>
                    Hello World!
                </div>
              )
          }
      }

      ReactDOM.render(
        <App />, 
        document.getElementById("app")
      );
    </script>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

更新:您需要polyfill才能在IE11中工作。