当前位置:网站首页>微信小程序:全局状态变量的使用
微信小程序:全局状态变量的使用
2022-06-13 06:04:00 【HaanLen】
全局变量的定义
在app.ts 文件中设置,globalData对象就是存储全局变量的。
// app.ts
App<IAppOption>({
globalData: {
userName: 'Yan'
},
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
console.log(res.code)
// 发送 res.code 到后台换取 openId, sessionKey, unionId
},
})
},
})
设置全局变量的值
<!--pages/study/index.wxml-->
<text>pages/study/index.wxml</text>
<view>{
{text}}</view>
<button type="primary" bindtap="getInfo">点击获取信息</button>
<view>yici</view>
<button type="primary" bindtap="setGlobal">设置全局变量</button>
setGlobal(){
app1.globalData.userName = 'Hello Yanpppp'
}
全局变量的使用
<!--pages/note.wxml-->
<text>pages/note.wxml</text>
<view>{
{text}}</view>
<view>ooo</view>
// pages/note.ts
const appNote = getApp();
Page({
/** * 页面的初始数据 */
data: {
text: appNote?.globalData.userName,
},
/** * 生命周期函数--监听页面加载 */
onLoad() {
},
/** * 生命周期函数--监听页面初次渲染完成 */
onReady() {
},
/** * 生命周期函数--监听页面显示 */
onShow() {
},
/** * 生命周期函数--监听页面隐藏 */
onHide() {
},
/** * 生命周期函数--监听页面卸载 */
onUnload() {
},
/** * 页面相关事件处理函数--监听用户下拉动作 */
onPullDownRefresh() {
},
/** * 页面上拉触底事件的处理函数 */
onReachBottom() {
},
/** * 用户点击右上角分享 */
onShareAppMessage() {
}
})
边栏推荐
- The technical analysis of ERP systems of the two camps in the world has been picked up many times.
- [to]12 common IP commands in the iproute installation package
- OpenGL马赛克(八)
- Test logiciel - résumé des FAQ d'interface
- Leetcode planting flowers - simple
- How to view tongweb logs correctly?
- Status management --provider
- What happens when the MySQL union index ABC encounters a "comparison operator"?
- USB debugging assistant
- 软件测试——接口常见问题汇总
猜你喜欢
Sentinel series hot spot current limiting
What happens when the MySQL union index ABC encounters a "comparison operator"?
Status management --provider
The difference between the increment and decrement operators before and after variables i+, +i, I –, – I
ffmpeg 下载后缀为.m3u8的视频文件
OpenGL马赛克(八)
中断处理过程
Etcd understanding of microservice architecture
Working principle of sentinel series (concept)
Sentinel series introduction to service flow restriction
随机推荐
Leetcode- minimum number of operations to make array elements equal - simple
1+1>2,Share Creators可以帮助您实现
Leetcode fizz buzz simple
零拷贝技术
Complete USB debugging assistant
18 flowable task manualtask and receivetask
The reason why the process cannot be shut down after a spark job is executed and the solution
DLL bit by bit
ffmpeg 下载后缀为.m3u8的视频文件
MySQL stored procedure
Nacos series registry principle and source code analysis
The Boys x PUBGMOBILE 联动火热来袭!来看最新游戏海报
Class conflicts caused by tongweb Enterprise Edition and embedded Edition
ArrayList loop removes the pit encountered
How slow is the application system on tongweb? How dead is it?
You still can't remotely debug idea? Come and have a look at my article. It's easy to use
2021.9.29 learning log MIME type
[automated test] cypress manual
= = relation between int and integer
Leetcode- detect uppercase letters - simple