vue-router 前端路由之编程式路由

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/AiHuanhuan110/article/details/89421838

如果要跳转一个路由,可以使用router-link的方式进行跳转,可是这样做有一定的局限性,假设,我们希望在进行路由跳转传入一些参数进去,这个时候,应该怎么操作呢?

为了解决这一个问题,vue-router就推出了编程式路由,以编程的形式解决路由的跳转问题

编程式路由的跳转常用的两种方式如下:

  1. 通过path去跳转

    this.$router.push({
        path: "/login"
    }); 
    
  2. 通过name去跳转

    methods:{
        backBtn(){
            this.$router.push({
                name: "login",
                // 传值
                query: {
               	 id: user.id
                }
            }
            );
            // this.$router.back();		//回退,类似history.back()
        }
    }
    
    

上面的两种方式都可以进行路由的跳转,但是它们的应用点不一样,区别主要是在路由的传值方式不一样

猜你喜欢

转载自blog.csdn.net/AiHuanhuan110/article/details/89421838