标签:find require 统一 mod log rip imp query 介绍
模块化是指讲一个大的程序文件,拆分成许多小的文件,然后将小文件组合起来。
ES6之前的模块化规范有:
模块功能主要由两个命令构成:export和import
//m1.js
export let school = ‘Study‘;
export function teach(){
console.log("day day up");
}
//引入m1.js模块内容
import * as m1 from "./src/js/m1.js";
console.log(m1);
//统一暴露 m2
let school = ‘Study‘;
function findJob(){
console.log("day day up");
}
export {school,findJob};
import * as m2 from "./src/js/m2.js";
console.log(m2);
//默认暴露 m3
export default {
school:‘Study‘;
change:function(){
console.log("day day up");
}
}
import * as m3 from "./src/js/m3.js";
console.log(m3);
m3.defautl.change();//"day day up"
//1.通用引入方式
import * as m1 from "./src/js/m1.js";
//2.解构赋值形式
import {school,teach} from "./src/js/m1.js";
import {school as height,findJob} from "./src/js/m2.js";
import {default as m3} from "./src/js/m3.js";
console.log(school,teach);//Study //"day day up"
console.log(height,findJob);
console.log(m3);
//3.简便形式 针对默认暴露
import m3 from "./src/js/m3.js";
console.log(m3);
//4.
//app.js 入口文件
//模块引入
import * as m1 from "./m1.js";
import * as m2 from "./m2.js";
import * as m3 from "./m3.js";
console.log(m1);
console.log(m2);
console.log(m3);
//index.html
<script src=‘./js/app.js‘ type="module" ></script>
//1.安装工具 babel-cli babel-preset-env browserify|webpack
npm install //初始化
npm i babel-cli babel-preset-env browserify -D
//2.npx babel src/js -d dist/js 设置转换目录,转换
npx babel src/js -d dist/js --presets-babel-preset-env
//3.打包 npx browserify dist/js/app,js -o dist/bundle.js
npx browserify dist/js/app,js -o dist/bundle.js
//index.html
<script src=‘dist/bundle.js‘></script>
//app.js
//修改背景颜色为粉色
import $ from ‘jquery‘;// const $ = require(‘jquery‘);
$("body").css(‘background‘,‘pink‘);
标签:find require 统一 mod log rip imp query 介绍
原文地址:https://www.cnblogs.com/AaronNotes/p/14373023.html