微信小程式封裝獨立公共js,其它頁面呼叫
阿新 • • 發佈:2018-12-18
//index.js var common = require("../../js/common.js"); Page({ /** * 頁面的初始資料 */ data: { shidu: "", //溼度 pm25: 0, //pm2.5 pm10: 0, //pm10 quality: "", //空氣質量 wendu: "", //溫度 ganmao: "", //感冒提醒(指數) show: false }, btnClick: function() { console.log("點選了按鈕 " + new Date()) }, view1Clcik: function(e) { console.log("view1Clcik " + new Date()) console.log(e); }, view2Clcik: function() { console.log("view2Clcik " + new Date()) }, view3Clcik: function(e) { console.log("view3Clcik " + new Date()) console.log(e); }, btnQuery: function() { var that = this; common.queryExp(function(result) { console.log(result); that.setData({ shidu: result.shidu, //溼度 pm25: parseFloat(result.pm25), //pm2.5 pm10: parseFloat(result.pm10), //pm10 quality: result.quality, //空氣質量 wendu: result.wendu, //溫度 ganmao: result.ganmao, //感冒提醒(指數) show: true }); }) }, inputChange: function(e) { console.log(e); } })
//common.js 封裝的函式 function queryExp(result) { wx.request({ url: 'http://t.weather.sojson.com/api/weather/city/101030100', method: 'get', data: {}, success: function (res) { var d = res.data.data; console.log(d); result(d); } }) } //轉化成小程式模板語言 這一步非常重要 不然無法正確呼叫 module.exports = { queryExp: queryExp };