hapiman / jsrice

A common utils using in javascript
0 stars 0 forks source link

module.exports & export default #10

Closed hapiman closed 7 years ago

hapiman commented 7 years ago

关联使用 module.exportsrequire export defaultimport 否则webpack打包的时候会出现Cannot assign to read only property 'exports' of object '#<Object>'错误


在前端使用这种通用的方式

//foo.js 
export default  funcname(){} 

import { funcname } from './foo.js'
funcname()
//或者使用 
import  * as  funs from './foo.js'
funs.funcname()
hapiman commented 7 years ago
hapiman commented 7 years ago

node文件中使用

exports=module.exports=someObj
exports.func1 = function(){}
exports.func2 = function(){}
hapiman commented 7 years ago

// foo.js module.exports = { bar() {} }

- works
```js
import foo from './foo';
foo.bar();

// foo.js
export default {
 bar() {}
}

// foo.js export default { bar() {} }

--- 
```js
export default {
  bar() {}
}

其实等价于

exports: {
  default: {
    bar() {}
  }
}

使用 import foo from './foo'; 其实得到

{
  default: {
    bar() {}
  }
}

会自动去找export default = {}