方法一:

pages/pre/pre.vue

<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>

pages/next/next.vue

<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>

方法二:

pages/pre/pre.vue

<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]; //当前页面
			let json = currPage.data.testdata;
			this.testdata = json;
		},
		methods: {
			goNext() {
				uni.navigateTo({
					url: '/pages/next/next'
				})
			}
		}
	}
</script>

<style>

</style>

pages/next/next.vue

<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>

 

Logo

前往低代码交流专区

更多推荐