本文共 874 字,大约阅读时间需要 2 分钟。
每个小程序都需要在 app.js 中调用 App 方法注册小程序示例,绑定生命周期回调函数、错误监听和页面不存在监听函数等。
整个小程序只有一个 App 实例,是全部页面共享的。开发者可以通过 getApp 方法获取到全局唯一的 App 示例,获取App上的数据或调用开发者注册在 App 上的函数。
测试
注册一个微信小程序 在app.js中添加// app.jsApp({ onLaunch (options) { // Do something initial when launch. }, onShow (options) { // Do something when show. }, onHide () { // Do something when hide. }, onError (msg) { console.log(msg) }, test:function() { //添加 console.log("my name is test and come from App.js"); }, globalData: { //添加 userInfo:null, helloFromApp:'Hello,I come from App.js' //---添加 }})
获取示例
开发者可以通过 getApp 方法获取到全局唯一的 App 示例,获取App上的数据或调用开发者注册在 App 上的函数。
// xxx.js
const appInstance = getApp()app.test(); //my name is test and come from App.jsconsole.log(appInstance.globalData) //Hello,I come from App.js
转载地址:http://qfzmf.baihongyu.com/