require
1、导入js或json文件
# inde.js
const hello= require('./me');
hello();
const message= require('./message');
console.log(message);
# me.js
function hello(){
console.log("hello123");
//print("hello");
}
module.exports = hello;//导出一个函数
# message.json
{
"name":"liu",
"address":"beijign"
}
执行 node.js
hello123
{ name: 'liu', address: 'beijign' }
2、导入一个模块
导入模块时默认在package.json 中找main 实际导入的它后面的名字
const { defineConfig } = require('@vue/cli-service')
# 实际导入的是node_modules/@vue/cli-service/lib/Service.js
# 这里的/lib/Service.js实际就是 package.json的name属性
所以也可以这样写
const { defineConfig } = require('@vue/cli-service/lib/Service.js')
import
# 导入模块内的所有属性
import * as xxx from "./xxx"
# html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<script type="module">
import * as xxx from "./people.js"
console.log(xxx);
</script>
</body>
</html>
# people.js 暴露方式一 这个可以按需获取 import name from "./people.js"
export let name="liu";
export function printName(){
console.log("my name is liu");
}
# peopl.js 暴露方式二 这个可以按需获取 import name from "./people.js"
let name="liu";
function printName(){
console.log("my name is liu");
}
export {name,printName}
# 默认暴露 #这个只针对默认暴露 import xxx from "./people.js"
export default {
name: "liu",
printName: function(){
console.log("------");
}
}
vscode装一个Live Server 然后右键open with live server可以看到效果