ES6的模块化

ES6的模块化

模块的思想,将对应的功能代码封装为一个模块(js代码 css代码 html代码)。

想要使用别人就导入,想要给别人用就导出。复用。

模块化的常用的模式

amd (在对应的加载之前导入)

cmd (在用的时候导入)

comment.js (基于amd和cmd之上)

es6的模块化的关键词(要想导入想要导出)

import 导入

export 导出

export的使用

第一种 export default (只能声明一次)

// //默认导出只有一个 如果使用export default 导出的可以使用对应的一个名字来接
export default {
   obj,
   str,
   say
}

  接收

import obj from './test.js'
console.log(obj) //{obj,str,say}

 第二种导出

//如果直接使用的对应的export 导出那么必须通过{键}来接
export const obj = {
    name:'jack',
    age:18
}
//如果导出的是值类型 一定要写变量名
export const str = "你好世界"
export const say = ()=>{
    console.log('hello world');
}

 

接收

 import {obj,str,say} from './test.js'

  

第三种导出

const name = 'tom'
//第三种导出
export {
    //只能写变量
    name
}

  

接收  

import {name} from './test.js'

  

import使用
import 名字 from '地址'

  

 

 

 

 

 

 

 

 

 

 

 

posted @ 2022-10-25 21:47  後楓浪  阅读(12)  评论(0)    收藏  举报