vue2路由跳转

本文介绍了在Vue.js中使用<router-link>进行无参和带参数的页面跳转,包括动态绑定和query、params的传递方式。同时,也讲解了编程式导航的实现,如使用this.$router.push和this.$router.replace进行路径操作,并展示了参数接收的方法。
摘要由CSDN通过智能技术生成

声明式< router-link>标签

  • 无参跳转

    • 字符串:
    <router-link to="/home">Home</router-link>
    
    • 动态绑定:
    <router-link :to="'home'">Home</router-link>
    
    上面home调用下面home
    
    export default {
      name: 'App',
      data(){
        return {
          home:'/home',
        }
      }
    }
    

2.携带参数

  • query

    <router-link :to="{path:'/home',query:{id}}">
    home
    </router-link>
    


    html取参

    $route.query.id
    

    js取参

    this.$route.query.id
    
  • params

    <router-link :to="{name:'home',params:{id}}">
    home
    </router-link>
    

    name是路由别名

    html取参

    $route.params.id
    

    js取参

    this.$route.params.id
    

编程式js方法

this.$router.push({ path: '/home' })
this.$router.push({path: '/home', query: {id:this.id}});
this.$router.push({ name: 'home', params: { id: this.id}})

接参方式同上


tip

this.$router.push

跳转到指定url路径,并向history栈中添加一个记录,点击后退会返回到上一个页面。

this.$router.replace

跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面,就是直接替换了当前页面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值