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() // 最后在这里生效
- })
- },
来源: http://www.bubuko.com/infodetail-2970058.html