微信小程序引用外部js

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() // 最后在这里生效
    })
  },

猜你喜欢

转载自www.cnblogs.com/sllzhj/p/10445814.html
今日推荐