router中push的几种用法

本文探讨了Vue Router中push方法的各种使用场景和技巧,详细解释了如何在main界面进行路由导航,帮助开发者更好地理解和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

路由设置部分


import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import main from '@/components/main'
import one from '@/components/one'
import two from '@/components/two'
import three from '@/components/three'


Vue.use(Router)

export default new Router({
	mode:'history',
  routes: [
    {
      path:'/',
	  redirect:'/main'
    },
	{
		path:'/main',
		component:main,
		children:[
			{
				path:'/one',
				component:one
			},
			{
				path:'/two/:id',
				name:'two',
				component:two
			},
			{
				path:'/three',
				name:'three',
				component:three
			}
		]
	}
  ]
})

main界面

<template>
	<div class="main">
		<button @click="btn1()">点击我实现页面跳转1</button>
		<button @click="btn2()">点击我实现页面跳转2</button>
		<button @click="btn3()">点击我实现页面跳转3</button>
		<router-view></router-view>
	</div>
</template>

<script>
export default{
	name:'main',
	methods:{
	    //所呈现形式为/one
		btn1(){
			this.$router.push({
				path:'/one'
			})
		},
		//所呈现形式为/two/123  取数据方式为 this.$route.params.id
		btn2(){
			this.$router.push({
				name:'two',
				params:{
					id:'123'
				}
			})
		},
		//上下两种前面一种需要去路由器里面配置:id     query可以直接进行参数设置不需要再去路由里面配置
		//所呈现形式为/three?id=444&name=zhangsan   取数据方式为 this.$route.query.id
		btn3(){
			this.$router.push({
				path:'/three',
				query:{
					id:'444',
					name:'zhangsan'
				}
			})
		}
	}
}
</script>

<style>
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值