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条记录

相关推荐
mantou1323 分钟前
从纯 CSR 到 SSR:用 WebComponents 做一个 Next.js 式框架 Gemkit
前端·全栈·web components
cidy_984 分钟前
深度拆解任务工作台架构:左右分栏、队列管理、实时刷新全方案
前端
Linye7 分钟前
告别“PRD 与原型两张皮”!我们把可交互原生组件直接嵌进了需求文档,还能双向回写 Markdown
前端
deli0077 分钟前
RSA 为什么公开公钥也解不开?2 个质数拆开实测一遍
前端
ModyQyW8 分钟前
vite-plugin-uni-pages 0.6:统一扫描规则,开发期少重启
前端·uni-app
门思科技13 分钟前
把 ThinkLink 接进 Home Assistant 与 Node-RED:一条 MQTT 转发器能走通的路
前端
没事写点啥25 分钟前
WeKnora 回答溯源
前端·后端·ai编程
LuHang1 小时前
Vue3 大屏项目 MQTT 实战:机场与无人机 OSD 实时数据总线
前端
2401_885885041 小时前
芬兰语音通知接口怎样对接?语音提醒接入流程
android·前端·python·php·语音识别
铁皮饭盒1 小时前
这6个小模型各有绝活, 且都可cpu/浏览器端运行
前端·后端