uniapp页面路由传参

最近做的项目碰到页面传参的问题,看文档办法觉得很麻烦,就试着用路由传参,效果如下

 

<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view class="">
			<button type="default" @tap="go('one')">我要到页面一</button>
			<!-- //括号内的参数要用单引号 ,在方法内带参数,-->
		</view>
		<view class="">
			<button type="default" @tap="go('tow')">我要到页面二</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {
        go(msg){uni.navigateTo({
        	url:'./select?data='+msg+''
        })
			// 将形参拼接带地址上,用?链接参数名data再加上形参msg
		}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
<template>
	<view class="">
		<view class="">
			<button type="default" @tap="to('red')">我要红色</button>
			<!-- //括号内的参数要用单引号 ,在方法内带参数,-->
		</view>
		<view class="">
			<button type="default" @tap="to('blue')">我要蓝色</button>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				msg:null,
				// 定义一个变量用于接受上个页面传来的参数
			}
		},
		onLoad:function select(msg){
			this.msg=msg.data
			console.log(this.msg)
		},
	   // onLoad生命周期函数,加载完页面后执行方法把上个页面传来的参数保存到变量里
		methods:{
			to(col){
				uni.navigateTo({
					url:'./'+this.msg+'?data='+col+''
				})
				// 把上个页面传来的参数作为字符串拼接到地址上,再加上本页面要传的参数
			}
		}
	}
</script>

<style>
</style>
<template>
	<view :class="this.msg">
		我是页面一
	</view>
</template>

<script>
	export default{
		data(){
			return{
				msg:null
			}
		},
		onLoad:function one(col){
			this.msg=col.data
		},
		
		
	
	}
</script>

<style>
	.red{
		background-color: red;
	}
	.blue{
		background-color: blue;
	}
	
</style>
<template>
	<view :class="this.msg">
		我是页面二
	</view>
</template>

<script>
	export default{
		data(){
			return{
				msg:null
			}
		},
		onLoad:function one(col){
			this.msg=col.data
		},
	}
</script>

<style>
	.red{
		background-color: red;
	}
	.blue{
		background-color: blue;
	}
</style>

小程序舒尔特汉字方格源码 :

小程序舒尔特方格源代码_我爱的昵称为什么都存在的博客-CSDN博客小程序舒尔特方格源代码,使用uniapp编写https://blog.csdn.net/weixin_50501118/article/details/125075453 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp中,可以使用Vue Router来进行。有几种不同的方式可以实现: 1. 使用params:通过在由地址中添加数来递数据。在由跳转时,可以使用`uni.navigateTo`或`uni.redirectTo`方法,并在URL中添加数。在目标页面中,可以通过`this.$route.params`来获取递的数值。 2. 使用query:通过在URL中添加查询数来递数据。在由跳转时,可以使用`uni.navigateTo`或`uni.redirectTo`方法,并在URL中添加查询数。在目标页面中,可以通过`this.$route.query`来获取递的数值。 3. 使用Vuex进行全局状态管理:通过在Vuex中存储需要递的数,可以在任何组件中获取和修改这些数。在由跳转时,可以直接修改Vuex中的数值,在目标页面中获取最新的数值。 通过以上三种方式,你可以在uniapp中实现Vue。请根据你的具体需求选择适合的方式进行实现。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [(vue面试题)什么是Vue.js中的计算属性(computed)和侦听属性(watch)?它们之间有什么区别?.txt](https://download.csdn.net/download/weixin_44609920/88229893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [uniapp页面](https://blog.csdn.net/weixin_50501118/article/details/124678534)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值