問題: html
在頁面上顯示當前時間(日期)npm
方法:api
一、在util.js (建立項目自動生成)中:函數
1 // util.js 2 const formatTime = date => { 3 const year = date.getFullYear() 4 const month = date.getMonth() + 1 5 const day = date.getDate() 6 const hour = date.getHours() 7 const minute = date.getMinutes() 8 const second = date.getSeconds() 9 10 return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':') //返回年月日,時分秒 11 } 12 13 const formatNumber = n => { 14 n = n.toString() 15 return n[1] ? n : '0' + n 16 } 17 18 module.exports = { 19 formatTime: formatTime 20 }
二、index.wxml 中寫一個text顯示時間ui
1 <text class="user-motto">{{time}}</text> this
三、index.js中寫邏輯spa
1 // index.js 2 3 var util = require('../../utils/util.js'); 4 Page({ 5 ...................... 6 /** 7 * 生命週期函數--監聽頁面加載 8 */ 9 onLoad: function (options) { 10 //日期顯示 11 var time = util.formatTime(new Date()) 12 //爲頁面中time賦值 13 this.setData({ 14 time: time 15 }) 16 }, 17 ............................ 18 })
四、重點:require 官方連接:https://developers.weixin.qq.com/minigame/dev/reference/api/require.htmlcode
any require(string path)
引入模塊。返回模塊經過
module.exports
或exports
暴露的接口。orm
名稱 類型 說明 path string 須要引入模塊文件相對於當前文件的相對路徑,或npm模塊名,或npm模塊路徑。不支持絕對路徑