方法一:
<template>
<view>
<view>返回的数据为:</view>
<view>id: {{testdata.id}}</view>
<view>name: {{testdata.name}}</view>
<button type="primary" @click="goNext">跳转到下一页面</button>
</view>
</template>
<script>
export default {
data() {
return {
testdata: {
id: '',
name: ''
}
}
},
onShow() {
let that = this
uni.$on('updateData',function(data){
that.testdata = data
const params = 'id:'+data.id+', name:'+data.name;
console.log('监听到事件来自 updateData ,携带参数为:' + params);
})
},
methods: {
goNext() {
uni.navigateTo({
url: '/pages/next/next'
})
}
}
}
</script>
<style>
</style>
<template>
<view>
<button type="primary" @click="goBack">点击返回上一页</button>
</view>
</template>
<script>
export default {
data() {
return {
mydata: {
id: 1,
name: 'test'
}
}
},
methods: {
goBack() {
uni.$emit('updateData', this.mydata)
uni.navigateBack({
delta: 1
})
}
}
}
</script>
<style>
</style>
方法二(相对来说,没有方法一简练):
<template>
<view>
<view>返回的数据为:</view>
<view>id: {{testdata.id}}</view>
<view>name: {{testdata.name}}</view>
<button type="primary" @click="goNext">跳转到下一页面</button>
</view>
</template>
<script>
export default {
data() {
return {
testdata: {
id: '',
name: ''
}
}
},
onShow() {
let pages = getCurrentPages();
let currPage = pages[pages.length - 1]; //当前页面
if(currPage.data != undefined){
let json = currPage.data.testdata;
this.testdata = json;
}
},
methods: {
goNext() {
uni.navigateTo({
url: '/pages/next/next'
})
}
}
}
</script>
<style>
</style>
<template>
<view>
<button type="primary" @click="goBack">点击返回上一页</button>
</view>
</template>
<script>
export default {
data() {
return {
mydata: {
id: 1,
name: 'test'
}
}
},
methods: {
goBack() {
var pages = getCurrentPages();
var prevPage = pages[pages.length - 2];
// #ifdef H5
prevPage.$vm.testdata = this.mydata;
// #endif
// #ifdef MP-WEIXIN
prevPage.setData(this.mydata);
// #endif
uni.navigateBack({//返回
delta: 1
})
}
}
}
</script>
<style>
</style>