小程序 globalData 详解

globalData的使用

  1. 作用:app.js中的globalData存储的是全局数据,能在各个页面之间共同使用某些信息,可以对共享数据进行修改设置,以便其他页面根据数据变化进行相应调整;
  2. 如何使用:
    globalData其他文章解释
    globalData社区观点讨论
    1)app.js中读取globalData,使用this即可。
    2)其他js页面使用,开头需要声明 var app = getApp()
    进行获取: globalData: app.globalData.name;
    进行修改: getApp().globalData.name=“王二麻子”,此时改的是全局变量的值,对本页面不起作用。要修改本页面的值,直接setData进行修改。
    代码示例:
// app.js
App({
  onLaunch: function () {
    var t = this
  },
  globalData:{
    targetPage: '',
    topheight: 50,
    topbottom: 60
  }
})

<!-- index.wxml -->
<view class="nav" style="height:{{topheight+topbottom}}rpx">
  <view class="battery" style="height:{{topheight}}rpx"></view>
  <view class="head-title" style="height:{{topbottom}}rpx">这是测试</view>
</view>
/* index.wxss */
.nav{
  height: 120rpx;
  background-color: rgb(221, 93, 61)
}
.head-title{
  text-align: center;
}

Page({
  data: {
    isBackFromPage2: !1,
    topheight: app.globalData.topheight,
    topbottom: app.globalData.topbottom
    },
    onLoad: function () {
    var t = this;
    // 以下两句注释,该页面的数据不会发生变化,但是全局变量topheight和topbottom的数据发生了改变
    // getApp().currentTarget.topheight = 60;     
    // getApp().currentTarget.topbottom = 60;
    t.setData({
      topheight: 60,
      topbottom: 60
    })
  }
    )}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序页面跳转传递值的几种方法如下: 1. 使用wx.navigateTo和wx.navigateBack函数的url参数传递值 在源页面使用wx.navigateTo函数跳转到目标页面时,可以在url参数中携带需要传递的值。例如: ``` wx.navigateTo({ url: '/pages/target/target?id=123&name=test' }) ``` 在目标页面的onLoad函数中可以获取这些值: ``` onLoad: function(options) { console.log(options.id) // 输出123 console.log(options.name) // 输出test } ``` 如果要在目标页面中修改这些值并返回到源页面,则可以使用wx.navigateBack函数的delta参数和wx.getCurrentPages函数获取源页面实例对象,然后调用实例对象的setData方法更新源页面数据。例如: 在目标页面中修改值并返回: ``` wx.navigateBack({ delta: 1, success: function(res) { let pages = getCurrentPages() let prevPage = pages[pages.length - 2] prevPage.setData({ id: 456, name: 'update' }) } }) ``` 2. 使用全局变量传递值 在app.js中定义一个全局变量,在需要传递值的页面中修改该变量的值,并在需要获取该值的页面中使用getApp函数获取app实例对象来获取该变量的值。例如: 在app.js中定义全局变量: ``` App({ globalData: { id: '', name: '' } }) ``` 在需要传递值的页面中修改该变量的值: ``` let app = getApp() app.globalData.id = 123 app.globalData.name = 'test' ``` 在需要获取该值的页面中获取该变量的值: ``` let app = getApp() console.log(app.globalData.id) // 输出123 console.log(app.globalData.name) // 输出test ``` 3. 使用本地存储传递值 在源页面中使用wx.setStorageSync函数将需要传递的值存储到本地存储中,在目标页面中使用wx.getStorageSync函数获取该值,并在需要修改该值并返回到源页面时使用wx.setStorageSync函数更新该值。例如: 在源页面中存储需要传递的值: ``` wx.setStorageSync('id', 123) wx.setStorageSync('name', 'test') ``` 在目标页面中获取该值: ``` let id = wx.getStorageSync('id') let name = wx.getStorageSync('name') console.log(id) // 输出123 console.log(name) // 输出test ``` 在目标页面中修改该值并返回: ``` wx.navigateBack({ delta: 1, success: function(res) { wx.setStorageSync('id', 456) wx.setStorageSync('name', 'update') } }) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值