小程式學習之旅---wxs模組自定義方法
阿新 • • 發佈:2018-12-17
// pages/user/user.js Page({ /** * 頁面的初始資料 */ data: { d: '1500000000000' }, goShop () { wx.navigateTo({ url: '../shop/shop' }) }, /** * 生命週期函式--監聽頁面載入 */ onLoad: function (options) { }, /** * 生命週期函式--監聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命週期函式--監聽頁面顯示 */ onShow: function () { }, /** * 生命週期函式--監聽頁面隱藏 */ onHide: function () { }, /** * 生命週期函式--監聽頁面解除安裝 */ onUnload: function () { }, /** * 頁面相關事件處理函式--監聽使用者下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函式 */ onReachBottom: function () { }, /** * 使用者點選右上角分享 */ onShareAppMessage: function () { } })
<!--pages/user/user.wxml--> <wxs src="../../wxs/tools.wxs" module="tools" /> <view class="" hover-class="none" hover-stop-propagation="false"> <button bindtap="goShop">點選跳轉shop頁面</button> </view> <view> {{tools.foo}} </view> <view> {{tools.bar('哈哈哈')}} </view> <wxs src="../../wxs/utils.wxs" module="utils" /> <view>{{utils.formatDate(d)}}</view>
/*模組*/ /* d是一個時間戳 1500000000000 轉化成 2018/5/24 */ var formatDate = function (oTime) { var d = date = getDate(parseFloat(oTime)); console.log(d) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':') } var formatNumber = function (n) { n = n.toString() return n[1] ? n : '0' + n } module.exports = { formatDate: formatDate };
/*模組*/
var foo = "'hello world' form tools.wxs";
var bar = function (d) {
return d;
}
module.exports = {
foo: foo,
bar: bar
};