require和import

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可以看到效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值