App()
App()
函数用来注册一个小程序。接受一个object参数,其指定小程序的生命周期函数等。app.js
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'
})
例如:
App({
onLaunch:function(options){
console.log("[onLaunch]场景值:",option.scene);
}
})
getApp()
全局的getApp()
函数,可以获取到小程序实例。
var appinstance=getApp()
console.log(appinstance.globalData)
注意:
App()
必须在app.js中注册,且不能注册多个。
不要在定义于App()
内的函数中调用getApp()
,使用this
就可以拿到app实例。
不要在onLaunch的时候调用getCurrentPage()
,此时page还没有生成。
通过getApp()
获取实例之后,不要私自调用生命周期函数。