1、先建立一個common.js, 寫我們的外部js
比如: common.js
function getTime(){ //下面寫我們的代碼 .... } function getCity(){ //下面寫我們的代碼 .... } module.exports = { //必須在這里暴露接口,以便被外界訪問,不然就不能訪問 getTime: getTime, getCity:getCity }
2、在需要引入js文件的js中寫
var commonJS= require("common.js"); onLoad: function (options) { this.setData({ time: commonJS.getTime() // 最后在這里生效 }) },
