小程式註冊程式,註冊頁面引數說明
阿新 • • 發佈:2018-12-19
1.註冊程式
App(Object)
App() 函式用來註冊一個小程式。接受一個 Object 引數,其指定小程式的生命週期回撥等。
App() 必須在 app.js 中呼叫,必須呼叫且只能呼叫一次。不然會出現無法預期的後果。
Object引數說明:
屬性 | 型別 | 描述 | 觸發時機 |
---|---|---|---|
Function | 生命週期回撥—監聽小程式初始化 | 小程式初始化完成時(全域性只觸發一次) | |
Function | 生命週期回撥—監聽小程式顯示 | 小程式啟動,或從後臺進入前臺顯示時 | |
Function | 生命週期回撥—監聽小程式隱藏 | 小程式從前臺進入後臺時 | |
Function | 錯誤監聽函式 | 小程式發生指令碼錯誤,或者 api 呼叫失敗時觸發,會帶上錯誤資訊 | |
Function | 頁面不存在監聽函式 | 小程式要開啟的頁面不存在時觸發,會帶上頁面資訊回撥該函式 | |
其他 | Any | 開發者可以新增任意的函式或資料到 Object 引數中,用 this 可以訪問 |
App({ onLaunch: function(options) { // Do something initial when launch. }, onShow: function(options) { // Do something when show. }, onHide: function() { // Do something when hide. }, onError: function(msg) { console.log(msg) }, globalData: 'I am global data' })
2.註冊頁面
頁面Page(Object)
Page(Object) 函式用來註冊一個頁面。接受一個 Object 型別引數,其指定頁面的初始資料、生命週期回撥、事件處理函式等。
Object 引數說明:
屬性 | 型別 | 描述 |
---|---|---|
data | Object | 頁面的初始資料 |
Function | 生命週期回撥—監聽頁面載入 | |
Function | 生命週期回撥—監聽頁面顯示 | |
Function | 生命週期回撥—監聽頁面初次渲染完成 | |
Function | 生命週期回撥—監聽頁面隱藏 | |
Function | 生命週期回撥—監聽頁面解除安裝 | |
Function | 頁面上拉觸底事件的處理函式 | |
Function | 頁面滾動觸發事件的處理函式 | |
Function | 當前是 tab 頁時,點選 tab 時觸發 | |
其他 | Any | 開發者可以新增任意的函式或資料到 Object 引數中,在頁面的函式中用 this 可以訪問 |
//index.js
Page({
data: {
text: "This is page data."
},
onLoad: function(options) {
// Do some initialize when page load.
},
onReady: function() {
// Do something when page ready.
},
onShow: function() {
// Do something when page show.
},
onHide: function() {
// Do something when page hide.
},
onUnload: function() {
// Do something when page close.
},
onPullDownRefresh: function() {
// Do something when pull down.
},
onReachBottom: function() {
// Do something when page reach bottom.
},
onShareAppMessage: function () {
// return custom share data when user share.
},
onPageScroll: function() {
// Do something when page scroll
},
onTabItemTap(item) {
console.log(item.index)
console.log(item.pagePath)
console.log(item.text)
},
// Event handler.
viewTap: function() {
this.setData({
text: 'Set some data for updating view.'
}, function() {
// this is setData callback
})
},
customData: {
hi: 'MINA'
}
})