项目结构
主页面
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>测试</title> <script type="module" src="index.js"></script> </head> <body> </body> </html>
index.js脚本
import {cruiser} from './toyato.js'; import {discovery} from './land-rover.js'; import {tire} from './tire.js'; console.log(`轮胎使用厂家数量: ${tire.count}`);
toyato.js
import {tire} from './tire.js'; tire.count++; export let cruiser = { name : 'Land Cruiser' };
land-rover.js
import {tire} from './tire.js'; tire.count++; export let discovery = { name : 'Land Rover Discovery' };
tire.js
// 导出一个轮胎对象 export let tire = { name : 'BRIDGESTONE', count : 0 };
运行结果