vue route 路由跳转传递参数

vue route 路由跳转传递参数

  • 使用 $router 方式

源组件:

<template>
    <button @click="sendParams"> 跳转 </button> 
</template>

<script>
export default {
    methods:{
        sendParams() {
            this.$router.push({
                path: 'target', 
                name: 'target' // 要跳转的路径的name, 在router文件夹下的 index.js文件内找,
                params: { 
                    name: 'value', 
                },
                query: {
                    id:10
                }
            })
        }
    }
}
</script>

这里是 $router.push() 操作
params, 是要传送的参数, 参数可以直接 k:v 形式传递
query, 是通过 url 来传递参数, 是 k:v 的格式

目标组件:

<script>
export default {
    methods:{
        getParams() {
           const params = this.$route.params;
           const query = this.$route.query;
           console.log(params, query)
        }
    }
    watch: {
        '$route': 'getParams'
    }
}
</script>

获取路由参数的时候使用的是 r o u t e . p a r a m s ∗ 和 ∗ route.params* 和 * route.paramsroute.query
使用 watch 监听器监测 $route 对象的变化, 一旦发生变动, 调用 getParams() 方法, 在此方法当中获取传递的参数, 并进行更新操作.

  • 使用 router-link

源组件:

<router-link 
    :to="{
        path: 'target',
        name: 'target'
        params: { 
            name: 'name', 
            data: data
        },
        query: {
            name: 'name', 
        }
    }">
</router-link>

参数和上面的 $router.push 当中的参数含义相同.

目标组件:

<script>
export default {
    methods:{
        getParams() {
           const params = this.$route.params;
           const query = this.$route.query;
           console.log(params, query)
        }
    }
    watch: {
        '$route': 'getParams'
    }
}
</script>

总结:

  1. 上述的两种方式是相同的效果, 其中参数 path, name 是必填参数.

  2. 上述的参数传递过程中, 在跳转到目标页面是没有刷新操作的.

vue 路由跳转

路由参数类型 Location, Route:

Location {
  name?: string;
  path?: string;
  hash?: string;
  query?: Dictionary<string>;
  params?: Dictionary<string>;
  append?: boolean;
  replace?: boolean;
}

Route {
  path: string;
  name?: string;
  hash: string;
  query: Dictionary<string>;
  params: Dictionary<string>;
  fullPath: string;
  matched: RouteRecord[];
  redirectedFrom?: string;
  meta?: any;
}
  • this.$router.push(Location, Callback)

在导航后会保存 history 记录. 当需要跳转的路由压栈.

  • this.$router.replace(Location, Callback)

Location 的 replace 属性为 true, 在导航后不会留下 history 记录. 即修改当前的路由为目标路由.

  • this.$router.go(N)

向路由栈当中PUSH(N为正数)或者POP(N为负数) N 个路由

  • this.$router.resolve(Location, Route)

解析路由, Location是需要跳转的位置, Route是当前的路由信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值