在模块中使用 require 传入文件路径即可引入文件
const test = require('./me.js');
案例:在func.js中定义一个函数,在index.js使用该函数
func.js
function test() {
console.log("hello 模块化")
}
//暴露数据
module.exports = test;
index.js
//导入模块
const test = require('./func');
//调用函数
test()
func.js
function test() {
console.log("hello 模块化")
}
function demo() {
console.log("hello demo")
}
//第一种方式:暴露数据 可以暴露 任意 数据
module.exports = {
test,
demo
};
//第二种方式:exports 不能使用 exports = value 的形式暴露数据
// exports.test = test;
// exports.demo = demo;
index.js
//导入模块
const func = require('./func');
//调用函数
func.test();
func.demo();
require 使用的一些注意事项: