在 uni-app 項目根目錄下創建 common 目錄,然后在 common 目錄下新建 helper.js 用於定義公用的方法。
const websiteUrl = 'http://uniapp.dcloud.io'; const now = Date.now || function () { return new Date().getTime(); }; const isArray = Array.isArray || function (obj) { return obj instanceof Array; }; export default { websiteUrl, now, isArray } 接下來在 pages/index/index.vue 中引用該模塊 <script> import helper from '../../common/helper.js'; export default { data() { return {}; }, onLoad(){ console.log('now:' + helper.now()); }, methods: { } } </script> 這種方式維護起來比較方便,但是缺點就是每次都需要引入。