Vue - 详解路由跳转及路由传参问题

一. 路由跳转有四种方式

  1. router-link

  2. this.$router.push() (函数里面调用)

  3. this.$router.replace() (用法同push)

  4. this.$router.go(n)

二. 不带参路由跳转

1. router-link

  • 路由配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    
  • 路由跳转

    <router-link :to="{name:'index'}">跳转</router-link>
    <router-link :to="{path:'/index'}">跳转</router-link>
    

2. this.$router.push()

  • 路由配置
    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    
  • 路由跳转
    this.$router.push('/index')
    this.$router.push({name:'index'})
    this.$router.push({path:'/index'})
    

3. this.$router.replace()

用法和this.$router.push()一样,push效果是进行跳转,replace可以理解为非跳转,而是替换当前路由地址

  • 路由配置
    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    
  • 路由跳转
    this.$router.replace('/index')
    this.$router.replace({name:'index'})
    this.$router.replace({path:'/index'})
    

4. this.$router.go(n)

  • 向前或者向后跳转n个页面,n可为正整数或负整数
  • n为正整,则向前跳转
  • n为负数,则向后跳转

二. 带参路由跳转

1. router-link

1.1. params 传参

  • 不配置路由path参数 ,第一次可请求获取参数,刷新页面,所传参数会消失

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    <router-link :to="{name:'index',params:{id:1}}">跳转</router-link>
    
  • 配置路由path参数,刷新页面,所传参数会保留

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index/:id',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    <router-link :to="{name:'index',params:{id:1}}">跳转</router-link>
    
  • 取参数

    两种方法可取:
    html 页面直接使用:$route.params.id
    script 取参:this.$route.params.id

    <template>
        <div>
            <span>Index页面</span>
            <p>html 取参:{{$route.params.id}}</p>
            <p>script 取参{{linkOne}}</p>
        </div>
    </template>
    <script>
    export default {
        data(){
            return{
                linkOne:0
            }
        },
        mounted(){
            this.linkOne=this.$route.params.id
        },
    }
    </script>
    <style scoped>
    </style>
    

1.2. query 传参

  • 路由path参数可以不配置,刷新页面,所传参数不会消失

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    <router-link :to="{name:'index',query:{id:1}}">跳转</router-link>
    
  • 取参数

    两种方法可取:
    html 页面直接使用:$route.query.id
    script 取参:this.$route.query.id

    <template>
        <div>
            <span>Index页面</span>
            <p>html 取参:{{$route.query.id}}</p>
            <p>script 取参{{linkOne}}</p>
        </div>
    </template>
    
    <script>
    export default {
        data(){
            return{
                linkOne:0
            }
        },
        mounted(){
            this.linkOne=this.$route.query.id
        },
    }
    </script>
    
    <style scoped>
    </style>
    

2. this.$router.push()

2.1. params 传参

  • 不配置路由path参数 ,第一次可请求获取参数,刷新页面,所传参数会消失

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    this.$router.push({name:'index',params: {id:'1'}})
    
  • 配置路由path参数,刷新页面,所传参数会保留

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index/:id',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    this.$router.push({name:'index',params: {id:'1'}})
    
  • 取参数

    两种方法可取:
    html 页面直接使用:$route.params.id
    script 取参:this.$route.params.id

    <template>
        <div>
            <span>Index页面</span>
            <p>html 取参:{{$route.params.id}}</p>
            <p>script 取参{{routerData}}</p>
        </div>
    </template>
    
    <script>
    export default {
        data(){
            return{
                routerData:0
            }
        },
        mounted(){
            this.routerData=this.$route.params.id
        },
    }
    </script>
    
    <style scoped>
    </style>
    

2.2. query 传参

  • 路由path参数可以不配置,刷新页面,所传参数不会消失

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    this.$router.push({name:'index',query: {id:'1'}})
    
  • 取参数

    两种方法可取:
    html 页面直接使用:$route.query.id
    script 取参:this.$route.query.id

    <template>
        <div>
            <span>Index页面</span>
            <p>html 取参:{{$route.query.id}}</p>
            <p>script 取参{{routerData}}</p>
        </div>
    </template>
    
    <script>
    export default {
        data(){
            return{
                routerData:0
            }
        },
        mounted(){
            this.routerData=this.$route.query.id
        },
    }
    </script>
    
    <style scoped>
    </style>
    

3. this.$router.replace()

  • 用法和this.$router.push()一样,push效果是进行跳转,replace可以理解为非跳转,而是替换当前路由地址

3.1. params 传参

  • 不配置路由path参数 ,第一次可请求获取参数,刷新页面,所传参数会消失

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    this.$router.replace({name:'index',params: {id:'1'}})
    
  • 配置路由path参数,刷新页面,所传参数会保留

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index/:id',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    this.$router.replace({name:'index',params: {id:'1'}})
    
  • 取参数

    两种方法可取:
    html 页面直接使用:$route.params.id
    script 取参:this.$route.params.id

    <template>
        <div>
            <span>Index页面</span>
            <p>html 取参:{{$route.params.id}}</p>
            <p>script 取参{{routerData}}</p>
        </div>
    </template>
    
    <script>
    export default {
        data(){
            return{
                routerData:0
            }
        },
        mounted(){
            this.routerData=this.$route.params.id
        },
    }
    </script>
    
    <style scoped>
    </style>
    

3.2. query 传参

  • 路由path参数可以不配置,刷新页面,所传参数不会消失

    路由参数配置

    { path: '/home',name: 'home',component: ()=>import('@/components/router/Home.vue') },
    { path: '/index',name: 'index',component: ()=>import('@/components/router/Index.vue') },
    

    路由传值跳转

    this.$router.replace({name:'index',query: {id:'1'}})
    
  • 取参数

    两种方法可取:
    html 页面直接使用:$route.query.id
    script 取参:this.$route.query.id

    <template>
        <div>
            <span>Index页面</span>
            <p>html 取参:{{$route.query.id}}</p>
            <p>script 取参{{routerData}}</p>
        </div>
    </template>
    
    <script>
    export default {
        data(){
            return{
                routerData:0
            }
        },
        mounted(){
            this.routerData=this.$route.query.id
        },
    }
    </script>
    
    <style scoped>
    
    </style>
    
  • 12
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值