如何导出一个类并在React.js中需要它

时间:2016-02-18 03:12:01

标签: javascript node.js

例如

component.jsx

var Hello = require('./component.jsx');
function main() {
    ReactDOM.render(<Hello />, document.getElementById('app'));
}

main.js

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

我使用webpack和module.exports,ES6导出工程查找。我只想知道如何以导出方式执行。

2 个答案:

答案 0 :(得分:2)

使用module.exports = class Hello extends React.Component { ... }

如果您使用exports.a,那么您需要撰写var Hello = require('./component.jsx').a(请注意.a

答案 1 :(得分:0)

使用以下语法:

export default class Hello extends React.Component {
  render() {
   return <h1 style={style1}>Hello world,{this.props.name}</h1>;
  }

您可以在此页面上阅读ES6导出语法: http://www.sitepoint.com/understanding-es6-modules/