第一种:url传值,在目标页面的onLoad(options)中接收

goDemoPage(){
    wx.navigateTo({
      url: `pages/demo/demo?id=${this.data.id}`,
    })
 },

demo.js

 onLoad(options) {
    console.log(options.id)
 }

第二种:通过全局存储,存放在app.js中,然后在其他页面进行获取使用、更改

app.js 代码

App({
    globalData:{
        userName: '嘻嘻哈哈'
    }
})

使用页面xxx.js

const appData = getApp();
page({
    onLoad(options) {
  // 获取app.js中globalData的值
  const userName = appData.globalData.userName;
  // 设置app.js中globalData的值
  appData.globalData.userName= '哈哈嘻嘻';
 }
})

第三种:通过本地存储,使用wx.setStorageSync()、wx.getStorageSync()

 // 存储
 wx.setStorageSync('userName', '嘻嘻哈哈')
 // 获取
 const userName =  wx.getStorageSync('userName')  //嘻嘻哈哈

第四种:通过自定义属性:data-×××。组件上触发的事件时,会发送给事件处理函数。

<scroll-view class="cates" scroll-x="true" scroll-with-animation="true" >
    <block wx:for="{{cates}}" wx:key="index">
      <view class="{{item.id === currentId?'cate-item-act cate-item':'cate-item'}}" data-id="{{item.id}}" bindtap="cateChange">{{item.name}}</view>
    </block>
</scroll-view>
cateChange(e){
    let id = e.currentTarget.dataset.id;
    this.setData({
      currentId: id
    })
  }

第五种:通过id标识传递

 <view wx:for="{{cates}}" wx:key="index" id="{{item.id}}" bindtap="demo">{{item.name}}</view>
 demo(e){
    console.log(e.currentTarget.id)
 }

 

posted on 2022-07-14 14:11  努力中的小白羊  阅读(73)  评论(0编辑  收藏  举报