工具庫的創建與使用
創建一個common文件夾
在common文件夾中創建一個utils文件夾
在utils文件夾中創建util.js
// 工具
function tool() {
console.log('i am a tool function.');
}
module.exports = {
tool: tool
}
在具體的頁面js中調用
首先要引入工具文件
const util = require('../../common/utils/util');
然后在具體的位置使用即可。
onLoad: function (options) {
util.tool();
...
}
i am a tool function.
這有點像,php中的common.php,里面有很多通用的函數庫。其實,不論是前端,還是后端。一些思想都是相通的。