有以下几种导入的方式:

  • import {xx} from 'xx';
  • import * as xx from 'xx';
  • import xx from 'xx';
  • var xx = require('xx');

import {xx} from ‘xx’;

与其对应的导出是:

  1. // 导出的3种方式
  2. export const name = 'jack'
  3. const age = 18
  4. export { age }
  5. const first = 'jim'
  6. export { first as firstName }
  7. // 导入
  8. import { name as fullName, age, firstName } from 'xx';

import * as xx from ‘xx’;

  1. // 导出
  2. export const name = 'jack'
  3. const age = 18
  4. export { age }
  5. const first = 'jim'
  6. export { first as firstName }
  7. // 导入
  8. import * as user from 'xx';
  9. // 使用
  10. console.log(user.age) // 18

import xx from ‘xx’

  1. // 导出
  2. export default function() {
  3. console.log('haha')
  4. }
  5. // 导入
  6. import Fn from 'xx';
  7. Fn() // 'haha'

require(‘xx’)

  1. // 导出
  2. module.export = {
  3. age: 18
  4. }
  5. module.export.name = 'jim'
  6. // 导入
  7. const user = require('xx')
  8. user.age // 18
  9. user.name // jim