module.exports

ReactJS 2017-04-12

用来导入某个js文件中的某一个模块。

What you need is a way to import only a specific part of a file into another file.

The answer is something called module.exports.module.exports comes from Node.js's module system, just like require does.

NOTE:Any component rendered by a different component must be included in module.exports.

Remember that every file is invisible to every other file, except for what you choose to expose via module.exports.
NavBar.js

var React = require('react');
var NavBar = React.createClass({
  render: function () {
    var pages = ['home', 'blog', 'pics', 'bio', 'art', 'shop', 'about', 'contact'];
    var navLinks = pages.map(function(page){
      return (
        <a href={'/' + page}>
          {page}
        </a>
      );
    });
    return <nav>{navLinks}</nav>;
  }
});
module.exports=NavBar;//

module.exports=NavBar;加上后,
Now, when ProfilePage.js uses requireto import NavBar.js, it will get back exactly what it wants: the NavBar component class.

ProfilePage.js
var React = require('react');
var ReactDOM = require('react-dom');
var NavBar = require('./NavBar');
var ProfilePage = React.createClass({
  render: function () {
    return (
      <div>
<NavBar />
        <h1>All About Me!</h1>
        <p>I like movies and blah blah blah blah blah</p>
        <img src="https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-monkeyselfie.jpg" />
      </div>
    );
  }
});

本文由 方方無 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论

shijiebei 365bet manbetx 188bet xinshui caipiao 95zz tongbaoyule beplay 88bifa 18luck betway bwin hg0088 aomenjinshayulecheng ca88 shenbotaiyangcheng vwin w88 weide