小程序頁面傳值的幾種方式
阿新 • • 發佈:2018-04-22
get 頁面傳值 targe eve url傳值 () pre itl 存取
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)
小程序頁面傳值的幾種方式