小程序页面传值的几种方式

1.  url传值

list.wxml:

 <view class="playIcon">
    <image src="../../iconfont/play_init.png"  bindtap="playAudio" data-songid="{{song.song_id}}"></image>
</view>


list.js:

playAudio: function (event) {
    let songid = event.currentTarget.dataset.songid;
    var that = this;
    wx.navigateTo({
      url: '/pages/listDetail/listDetail?title=' + songid,
    })
}    

listDetail:

 onLoad: function (options) {
    var that = this;
    console.log('options',options) 

}

2.app.globalData  设置全局变量

app.js: 设置全局变量

App({
globalData: {
userInfo: null,
host:'http://localhost:8000'
}
})
 

index.js:

const app = getApp()
app.globalData = '这里也可以设置值',
console.log(app.globalData.host)

3.setStorage() /getStorage() 将值写在本地缓存里,最大支持10M,可以存些文本之类的,音频视频就算啦

list.js:

存值到本地缓存

wx.setStorage('title',data)

 

listDetail.js:

从本地缓存取值

let info = wx.getStorage('title')

console.log('info',info)

猜你喜欢

转载自www.cnblogs.com/aloehui/p/8907116.html
今日推荐