微信小程序中全部 js 文件做用域皆爲獨立的,每個 js 文件即爲一個模塊。模塊與模塊之間的引用經過 module.exports 或 exports 對外暴露接口。node
注意:小程序
// common/tool.js =============================== function Hello(){ console.log("say hello!"); } function sayHi(){ console.log("Hi! I'm mirage. how are you"); } module.exports.Hello = Hello; exports.sayHi = sayHi; // index/index.js =============================== var tool = require("../common/tool.js"); Page({ onLoad:function(){ tool.Hello(); // 輸出 say hello! tool.sayHi(); // 輸出 Hi! I'm mirage. how are you })
引用模塊也是 require(path) 官方註明:require 暫不支持絕對路徑。微信小程序