计算机网络/计算机科学与应用/系统/运维/开发

微信小程序逻辑层-App/Page/getApp/getCurrentPages/模块

App程序注册

Page页面注册

getApp获取App实例

getCurrentPages获取当前页面栈


1、App方法

https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/app.html

image.png


一个小程序只有1个App实例,全部页面共享,可以通过getApp方法获取全局唯一的App实例,定义全局变量方法:

image.png


调用全局变量:

image.png


2、Page方法

https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html

image.png


调用自定义方法需要使用this

this.方法名称()


3、getApp方法

用于获取全局唯一的App实例,针对某个page
const app = getApp()


4、getCurrentPages方法

image.png


5、模块

将公用程序抽离 成一个独立的js文件,称为模块

模块通过module.exports或者exports对外暴露接口 ,exports是module.exports的一个引用

一般推荐使用module.exports

操作:在小程序根目录下创建util/util.js并写入代码

image.png

在使用的逻辑页面中引入公共方法:

image.png


好的习惯,排名第一的是:自律;排名第二的是:终身学习;排名第三的是:保持运动。拥有这三种习惯,美好人生垂手可得。

评论

^