#前端# 点击列表页跳转到对应的详情页(获取上一页的参数/id)

26 篇文章 5 订阅
17 篇文章 3 订阅

#前端#

点击列表页跳转到对应的详情页(获取上一页的参数/id)

示例:

从列表页跳转到详情页

效果图:

思路:

在列表项页面:

(1)首先通过后端接口数据,将列表页的内容进行循环渲染出来

(2)再通过绑定点击事件getDetail(),在事件里面传入id实参(就是后端返回的数据id)

示例:

<view v-for="(item, index) in reportList"  @click="toDetail(item.id)" >

(3)再在method里面的getDetail()里面,用一个形参来接收实参 

	methods: {
			
			toDetail(id) {
				
			}
		},

这个时候getDetail(id)就已经接收到后端返回的数据了

(4)然后把id带上,跳转到详情页

携带id的格式是:'../rick/rick? id='+id (这个格式里面,不能存在空格)

相当于:http://137..20.1:8081/system/poseport/709

toDetail(id) {
                uni.navigateTo({
                    url:'./risk/risk?id='+id
                })
            }

在详情页页面:

(1)在onload()里面传入一个option (与data同级)

( // option为object类型,会序列化上个页面传递的参数 )

data() {
		return {	
			}
		},
onLoad(option) {
	  
		}

(2)然后就在事件里面(getRisklist()这个事件,

就是渲染出详情页页面内容的事件,自定义的),拿到上一个页面的id :option.id


onLoad(option) {
	   this.getRisklist(option.id)
		}

(3)将option.id(实参)传递给到method里面的事件 getRisklist(id) (形参)

methods: {
			//获取各项风险详情
			getRisklist(id) {
				this.$ajax.get("/system/posturortdata/"+id)
					.then(res => {
						if (res.code == 200) {
							
						}
					})
			}
		}

(4)再在接口地址加上id(就是把刚刚拿到的id),然后再将后端接口数据,通过变量进行接收

地址接口加id格式 (也是一样,不能有任何空格)

("/system/posturtdata/"+id)
<script>
	export default {
		data() {
			return {
				baseUrl: this.$ajax.baseURL,
				list: [],
				bodyImg: '',
				rickImg: '',
				video: '',
				symptomName: '',
				num: ''
			}
		},
		onLoad(option) {
			this.getRisklist(option.id)
		},
		methods: {
			//获取各项风险详情
			getRisklist(id) {
				this.$ajax.get("/system/posturertdata/"+id)
					.then(res => {
						if (res.code == 200) {
							this.bodyImg = res.data.roleCutPhoto;
							this.rickImg = res.data.symptomPhoto;
							this.video = res.data.symptomVideo;
							this.symptomName = res.data.roleSymptomName;
							this.num = res.data.roleSymptomValue;
							this.list = res.data.postureRiskList
						}
					})
			}
		}
	}
</script>

(5)然后在标签里面进行渲染就可以了

<template>
	<view>
		<view class="header">
			<view class="item-left">
				<image :src="baseUrl + bodyImg"></image>
				<view class="title">{{symptomName}}</view>
			</view>
			<view class="item-right">
				<image :src="baseUrl + rickImg"></image>
				<view class="item-type">
					<view class="item-type-nums" :style="{'margin-left':num + '%' }">{{num}}</view>
					<view class="item-type-bg">
						<image src="@/static/health/line.png"></image>
						<view class="line-round" :style="{'left':num + '%' }"></view>
					</view>
					<view class="item-line-tip">
						<span>低</span>
						<span>中</span>
						<span>高</span>
					</view>
				</view>
			</view>
		</view>
		<view class="symptom">
			<image src="@/static/health/tt.png"></image>
			<view class="tips">症状解读</view>
		</view>
		<view class="item-video">
			<video :src="baseUrl +video" controls="true"></video>
		</view>
		<view class="symptom">
			<image src="@/static/health/tt.png"></image>
			<view class="tips">引发症状</view>
		</view>
		<view class="trigger" v-for="item in list">
			<view class="item">
				<view class="title">{{item.riskName}}</view>
				<view class="tip">{{item.riskDescribe}}</view>
			</view>
		</view>
	</view>
</template>
以下是一个前端模拟淘宝商品详情页的示例代码: ```html <!DOCTYPE html> <html> <head> <title>商品列表</title> <style> /* CSS样式省略 */ </style> </head> <body> <h1>商品列表</h1> <ul> <li><a href="detail.html?id=1">商品1</a></li> <li><a href="detail.html?id=2">商品2</a></li> <li><a href="detail.html?id=3">商品3</a></li> <!-- 其他商品列表项 --> </ul> </body> </html> ``` ```html <!DOCTYPE html> <html> <head> <title>商品详情页</title> <style> /* CSS样式省略 */ </style> <script> window.onload = function() { // 获取URL中的id参数 var urlParams = new URLSearchParams(window.location.search); var id = urlParams.get('id'); // 根据id获取商品数据并展示 var productData = getProductData(id); document.getElementById('product-image').src = productData.image; document.getElementById('product-name').innerText = productData.name; document.getElementById('product-price').innerText = productData.price; // 其他商品参数信息的展示 // 设置吸顶菜单 window.onscroll = function() { var menu = document.getElementById('sticky-menu'); if (window.pageYOffset > 200) { menu.classList.add('sticky'); } else { menu.classList.remove('sticky'); } }; // 快速回到顶部按钮 document.getElementById('back-to-top').onclick = function() { window.scrollTo(0, 0); }; // 加入购物车和立即购买按钮的点击事件 document.getElementById('add-to-cart').onclick = function() { // 添加到购物车的逻辑 }; document.getElementById('buy-now').onclick = function() { // 立即购买的逻辑 }; }; // 根据id获取商品数据的函数 function getProductData(id) { // 根据id从后端获取商品数据的逻辑 // 返回商品数据对象 } </script> </head> <body> <h1>商品详情页</h1> <div> <img id="product-image" src="" alt="商品图片"> <h2 id="product-name"></h2> <p id="product-price"></p> <!-- 其他商品参数信息的展示 --> </div> <div id="sticky-menu"> <button id="back-to-top">回到顶部</button> <button id="add-to-cart">加入购物车</button> <button id="buy-now">立即购买</button> </div> </body> </html> ``` 这个示例代码中,商品列表中的每个商品都有一个链接,链接的href属性中包含了商品的id参数。当用户点击某个商品时,会跳转到商品详情页,并将对应id参数传递给商品详情页。商品详情页根据id参数获取商品数据,并展示在面上。面中还包含了吸顶菜单、快速回到顶部按钮、加入购物车按钮和立即购买按钮等功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值