基于 React CDN 的页面不适用于 Babel 6.1.19

IT技术 javascript reactjs
2021-04-30 08:04:46

以下 React 代码在 Babel-Core CDN 更改为 5.8.24 版本时有效,但在包含的版本为 6.1.19 时无效。我很困惑为什么这很重要?事情不应该是向前兼容的吗?或者我只是在这里遗漏了一些非常明显的东西?

提前致谢。

<html>
<head>
    <script src="https://unpkg.com/react@15/dist/react.min.js">
</script>
    <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js">
</script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-
core/6.1.19/browser.min.js"></script>
</head>
<body>
    <div id="app"></div>
    <script type="text/babel">
        const HelloMessage = React.createClass({
            render: function() {
                return <div>Hello {this.props.name}</div>;
            }
        });

        ReactDOM.render(<HelloMessage name="John" />, 
document.getElementById('app'));
    </script>
</body>
</html>
1个回答

使用这个脚本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>

检查babel-standalone 文档

检查工作代码:

<!DOCTYPE html>
<html>
    <head>
       <script src="https://unpkg.com/react@15/dist/react.min.js"></script>
       <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
       <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
    </head>
    <body>
       <div id="app"></div>
           <script type="text/babel">
              const HelloMessage = React.createClass({
                 render: function() {
                     return <div>Hello {this.props.name}</div>;
                 }
              });

              ReactDOM.render(<HelloMessage name="John" />, 
                  document.getElementById('app'));
          </script>
    </body>
</html>