index.wxml
<view class="maxWidth">
<navigator url="/pages/home/home">view控件跳转</navigator>
<button bindtap="startHome" type="primary">js跳转</button>
<navigator url="/pages/home/home?indexData=我是个传递的数据">view控件跳转(参数传递)</navigator>
<button bindtap="startHome" type="primary">js跳转(参数传递)</button>
<text>{{homeData}}</text>
<image src="{{imageData}}"></image>
<button bindtap="startHome" type="primary">选择头像</button>
</view>
index.js
// index.js
// 获取应用实例
const app = getApp()
Page({
data:{
homeData:'',
imageData:'/static/gm.jpg'
},
startHome(){
wx.navigateTo({
url: '../home/home?indexData=我是个传递的数据'
})
}
})
home.wxml
<!--pages/home/home.wxml-->
<text>传递的参数是:</text>
<text>{{indexData}}</text>
<button bindtap="backto" type="primary">回传</button>
<text>点击头像即可切换</text>
<scroll-view class="srcolbox" scroll-x >
<block wx:for="{{imgs}}">
<image class="item" src="{{item}}" bindtap="imgOnClick" data-src="{{item}}"/>
</block>
<image class="item" bindtap="buttonOnClick" src="/static/rabbit.jpg"/>
</scroll-view>
home.js
// pages/home/home.js
Page({
/**
* 页面的初始数据
*/
data: {
indexData:'',
imgs:['/static/gm.jpg','/static/rabbit.jpg','/static/tiger.jpg','/static/zebra.jpg']
},
imgOnClick(src){
console.log(src.target.dataset.src);
let pages = getCurrentPages()
let prepage = pages[pages.length - 2]
prepage.setData({
imageData:src.target.dataset.src
})
wx.navigateBack({ })
},
onLoad(e){
this.setData({
indexData:e.indexData
})
},
//回传参数
backto(){
//拿到小程序当前运行的数据
let pages = getCurrentPages()
// pages.length - 1 为当前页面,所以要减2
let prepage = pages[pages.length - 2]
//调用上一个对象的参数
prepage.setData({
homeData:'回传的数据'
})
//返回上一个页面
wx.navigateBack({ })
}
})