使用RequireJS加载时未定义React

时间:2018-01-09 12:42:51

标签: javascript reactjs requirejs

鉴于下面的JavaScript:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script>
<script type="text/javascript">
    require.config({
        paths: {
            'react': 'https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min',
            'react-dom': 'https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min'
        }
    });
    require(['react', 'react-dom'], function () {
        var x = React;//Error - React is not defined
    });
</script>

为什么我在控制台React is not defined中收到错误?

enter image description here

1 个答案:

答案 0 :(得分:3)

您应该声明要导入的模块:

requirejs(['react', 'react-dom'], function(React, ReactDOM) { })

以下是一个工作示例:https://jsfiddle.net/remarkablemark/mejyoLk6/?utm_source=website&utm_medium=embed&utm_campaign=mejyoLk6