简体   繁体   English

具有React未捕获参考错误的WebPack

[英]WebPack with React Uncaught Reference Error

I'm very loosely following the example here up until the point where it starts running the dev server. 我非常松散地遵循这里的示例,直到它开始运行dev服务器。

I have a test React component (in scripts/test.jsx): 我有一个测试React组件(在scripts / test.jsx中):

/** @jsx React.DOM */

var React = require('react');

var Test = React.createClass({
    render: function(){
        return <h1>HI!</h1>
    }
});

module.exports = Test;

I have a webpack.config where I'm using the jsx loader against the source directory (It's basically the same as the one in the example except I'm adding the library properties ). 我有一个webpack.config,我在源目录中使用jsx加载器(除了我添加库属性之外,它与示例中的基本相同)。

I run webpack and it generates the bundle file like I expect, however, when I try to use the component in an html file (after including the bundle.js script reference) I get the following in console: " Uncaught ReferenceError: Test is not defined ". 我运行webpack并生成捆绑文件,就像我期望的那样,当我尝试在html文件中使用该组件时(包括bundle.js脚本引用之后),我在控制台中得到以下内容:“ 未捕获的ReferenceError:测试不是定义 “。

The HTML contains the following: HTML包含以下内容:

<div id="hi">
</div>
<script type="text/jsx">
    /** @jsx React.DOM */
    React.renderComponent(
      <Test />,
      document.getElementById('hi')
    );
</script>

Am I doing something incorrect to use a component that is defined in CommonJS style against an html page that isn't technically using a module loader (I'm trying to treat this test as if it's someone who is trying to load the component without any type of structured module loader)? 我是否做了一些不正确的事情,使用CommonJS样式定义的组件与技术上不使用模块加载器的html页面(我试图将此测试看作是试图加载组件而没有任何人结构化模块加载器的类型)?

The output of the webpack is available here webpack的输出可在此处获得

EDIT 2: The full example code is available as a github repo 编辑2:完整的示例代码可用作github仓库

Yeah, you'd be better off following the example and requiring Test from a .jsx file rather than inlining it in the HTML. 是的,你最好关注这个例子并要求从.jsx文件中Test而不是在HTML中内联它。

Otherwise, Test needs to be exported as a global, so you'd need to follow similar conventions to the browserify --standalone flag, which looks to be something like this: 否则, Test需要作为全局导出,因此您需要遵循类似的--standalone标志,看起来像这样:

output: {
    library: "Test",
    libraryTarget: "umd"
}

http://webpack.github.io/docs/webpack-for-browserify-users.html#standalone http://webpack.github.io/docs/webpack-for-browserify-users.html#standalone

Edit: After looking at your GH repo, you have: 编辑:看完你的GH回购后,你有:

<script src="bundle.js" type="text/js"></script>

instead of 代替

<script src="bundle.js" type="text/javascript"></script>

so it wasn't loading bundle at all. 所以它根本没有加载捆绑。 Further, you can't have 2 separate copies of react, the way you have it currently you're requiring React from within webpack, and then you're also loading it on the page. 此外,你不能有2个单独的反应副本,你目前在webpack中需要React的方式,然后你也在页面上加载它。 You should either export a function which takes the React object, or use the global to be able to use it outside of the bundle. 您应该导出一个获取React对象的函数,或者使用global来在bundle之外使用它。

For example this would work: 例如,这将工作:

/** @jsx React.DOM */
module.exports = function(React) {

var Test = React.createClass({
    render: function(){
        return <h1>HI!!</h1>
    }
});

return Test;
};

and then: 然后:

    /** @jsx React.DOM */
    React.renderComponent(
      Test(React)(),
      document.getElementById('hi')
    );

or this 或这个

/** @jsx React.DOM */
var Test = React.createClass({
    render: function(){
        return <h1>HI!!</h1>
    }
});

module.exports = Test;

and then: 然后:

    /** @jsx React.DOM */
    React.renderComponent(
      <Test />,
      document.getElementById('hi')
    );

Though I can't imagine most folks consuming a React package are going to be loading it with a script tag, and you generally don't want globals, so it's probably best to just use the CommonJS style require and let people figure out shimming or whatever. 虽然我无法想象大多数使用React包的人都会用脚本标签加载它,而你通常不想要全局变量,所以最好只使用CommonJS样式要求让人们弄清楚填充或者随你。

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

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