Vue编程式路由导航

目录

一、使用

不使用<router-link>标签,利用$router中的api实现跳转,更灵活

html 复制代码
<template>
	<div>
		<ul>
			<li v-for="m in messageList" :key="m.id">
				<!-- 跳转路由并携带params参数,to的字符串写法 -->
				<!-- <router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{m.title}}</router-link>&nbsp;&nbsp; -->

				<!-- 跳转路由并携带params参数,to的对象写法 -->
				<router-link :to="{
					name:'xiangqing',
					query:{
						id:m.id,
						title:m.title
					}
				}">
					{{m.title}}
				</router-link>
				<button @click="pushShow(m)">push查看</button>
				<button @click="replaceShow(m)">replace查看</button>
			</li>
		</ul>
		<hr>
		<router-view></router-view>
	</div>
</template>

<script>
	export default {
		name:'Message',
		data() {
			return {
				messageList:[
					{id:'001',title:'消息001'},
					{id:'002',title:'消息002'},
					{id:'003',title:'消息003'}
				]
			}
		},
		methods: {
			pushShow(m){
				this.$router.push({
					name:'xiangqing',
					query:{
						id:m.id,
						title:m.title
					}
				})
			},
			replaceShow(m){
				this.$router.replace({
					name:'xiangqing',
					query:{
						id:m.id,
						title:m.title
					}
				})
			}
		},
	}
</script>

this.$router.push()是压栈操作,可以回到之前的所有历史记录
this.$router.replace()是替换操作,不能回到上一次的历史记录
this.$router.back()回退一条记录,与history.back()的使用方式一致
this.$router.forward()前进一条记录,与history.forward()的使用方式一致
this.$router.go()与history.go()的使用方式一致

① go(0):刷新当前页面

② go(负整数):后退n条记录

③ go(正整数):前进n条记录

相关推荐
用户5372312882467 分钟前
你的 WGSL 从未被执行过——一个 secure context 静默陷阱的排查实录
前端
Daniel_12316 分钟前
轻量级站点监控面板 LightPing 开源啦
前端·后端·github
想风17 分钟前
A/B 测试怎么做?独立站转化率优化的完整实操步骤
前端·后端·github
lerhxx23 分钟前
从 Markdown 到生成式 UI:AI 应用中的流式渲染实践
前端·javascript
迅猛龙办公室23 分钟前
python实现简单进度条
java·前端·python
斯内普吖28 分钟前
(开源)宠物饲养系统实战指南 基于 Java + SSM + Vue + MySQL
java·vue.js·mysql·开源·宠物
Rosanci33 分钟前
Codex 下载与本地部署实战:从安装到运行全流程指南
开发语言·前端·算法·chatgpt·codex
zhangzeyuaaa38 分钟前
深入 Ruby:Block、Proc、Lambda 核心区别与最佳实践
开发语言·前端·ruby
liangshanbo12151 小时前
前端高级面试题:WebSocket 双向流式通信怎么设计?
前端·websocket·网络协议
可乐鸡翅yeah_1 小时前
新手梳理:HLS 线上问题,哪些该提给 CDN,哪些该找后端切片服务
开发语言·前端·javascript·vue.js·网络协议·http·m3u8在线