ES6和Node.js的import和export
<p>记录一下import和export的几种写法。</p><h3><span style="font-size: 16px">1.ES6的导入和导出</span></h3>
<p>0.入口文件为index.js,引用add-content.js的内容</p>
<p>1. export default 方式,直接导出变量</p>
<p>add-content.js的内容如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> write() {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> document.write('Hello World'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 255, 1)">var</span> app =<span style="color: rgba(0, 0, 0, 1)"> {}
</span><span style="color: rgba(0, 128, 128, 1)">6</span> app.write =<span style="color: rgba(0, 0, 0, 1)"> write
</span><span style="color: rgba(0, 128, 128, 1)">7</span>
<span style="color: rgba(0, 128, 128, 1)">8</span> export <span style="color: rgba(0, 0, 255, 1)">default</span> app;</pre>
</div>
<p>index.js引用要这样写</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> import app from './add-content'
<span style="color: rgba(0, 128, 128, 1)">2</span> app.write()</pre>
</div>
<p>2. export { } 方式,适合同时导出多个变量</p>
<p>add-content.js的内容如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> write() {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> document.write('Hello World'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">var</span> app =<span style="color: rgba(0, 0, 0, 1)"> {}
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> app.write =<span style="color: rgba(0, 0, 0, 1)"> write
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">export {
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)">app
</span><span style="color: rgba(0, 128, 128, 1)">10</span> }</pre>
</div>
<p>index.js引用要加上引号,如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> import { app } from './add-content'
<span style="color: rgba(0, 128, 128, 1)">2</span> app.write();</pre>
</div>
<p>3. export与变量声明的简写方式</p>
<p>add-content.js的内容如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> write() {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> document.write('Hello World'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> export <span style="color: rgba(0, 0, 255, 1)">var</span> app =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 0, 1)">write: write
</span><span style="color: rgba(0, 128, 128, 1)">7</span> }</pre>
</div>
<p>index.js引用要加上引号,如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> import { app } from './add-content'
<span style="color: rgba(0, 128, 128, 1)">2</span> app.write();</pre>
</div>
<h3>2.nodejs的导入和导出</h3>
<p>0.入口文件为index.js,引用mock.js的内容</p>
<p>1.mock.js中使用module.exports方式导出</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> module.exports =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> name: 'mock.js'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">3</span> message: 'hello world'
<span style="color: rgba(0, 128, 128, 1)">4</span> }</pre>
</div>
<p>index.js引用要这样写</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> let mock = require('./mock'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">2</span> console.info(mock.name)</pre>
</div>
<p>2.mock.js中直接使用exports导出</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> exports.name = 'mock'
<span style="color: rgba(0, 128, 128, 1)">2</span> exports.message = 'hello world'</pre>
</div>
<p>index.js引用要这样写</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> let mock = require('./mock'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">console.info(mock.name)
</span><span style="color: rgba(0, 128, 128, 1)">3</span> console.info(mock.message)</pre>
</div>
<p>其原理是将exports指向了module.exports,而module.exports在初始化时是一个空对象。</p>
<p>相当于在导出的文件里面添加了如下代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">var</span> module =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">exports: {}
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">var</span> exports = module.exports</pre>
</div>
<p>3.导出时容易出现的两个错误</p>
<p>3.1 给exports赋值,如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> exports =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> name: 'mock'
<span style="color: rgba(0, 128, 128, 1)">3</span> }</pre>
</div>
<p>由第2部分知:exports是指向了module.exports,如果重新给exports赋值,它会指向新对象,而module.exports还是空对象。</p>
<p>3.2 module.exports和exports混用</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> exports.name = 'mock'
<span style="color: rgba(0, 128, 128, 1)">2</span> exports.message = 'hello world'
<span style="color: rgba(0, 128, 128, 1)">3</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> module.exports =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 0, 1)">getName () {
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 255, 1)">return</span> 'get name function'
<span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">8</span> }</pre>
</div>
<p>由于对module.exports进行了赋值,所以exports方式添加的属性就会丢失,即访问不到name和message属性。</p>
<p> </p>
<p>个人建议使用module.exports = { ... } 导出会比较清晰。</p>
<h3>总结一下:</h3>
<p><strong>ES6 Moudle</strong>:import / export</p>
<p>浏览器使用也需要Babel的支持</p>
<p><strong>CommonJS</strong>: require / module.exports or exports</p>
<p>node的模块化规范,浏览器使用时需要用browserify解析</p>
<p><strong>AMD</strong>: require / defined</p>
<p>是requireJS的规范 define(['./a', './b'], function(a, b) { //... })</p>
<p> require是同步导入,支持动态导入,是值拷贝,导出值不会影响导入值;</p>
<p>import是异步导入,导入值会随导出值变化</p>
<p>备忘~</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/jyughynj/p/12077674.html
頁:
[1]