vue路由切换与传参总结

1.路由跳转
1.1编程式导航
官网:

在这里插入图片描述

<button 
          class="sui-btn btn-xlarge btn-danger" 
          type="button" 
          @click="goSearch">
            搜索
</button>
methods:{
    //搜索按钮的回调函数,需要向search路由进行跳转
    goSearch(){
    //编程式导航:想search路由进行跳转
   		this.$router.push("/search")
   }

1.2声明式导航
官网
在这里插入图片描述

 <router-link to="/home" class="logo" >
          <img src="./images/logo.png" alt="" />
  </router-link>

2.路由传参(params传参,query传参)
2.1编程式
2.1.1(非props)
通过params传参需要在配置路由时占位

{
      path:"/search/:keyword?",//params:路由传参,需要在路径中进行占位
      component:Search,
      meta:{
        showFooter:true
      },
      name:"search",
    },
//传参第一种:字符串形式
      // this.$router.push("/search/"+this.keyword)//param传参
      // this.$router.push("/search/"+this.keyword+"?k="+this.keyword.toUpperCase())//query传参
    
 //传参第二种:模板字符串
      // this.$router.push(`/search/${this.keyword})//param传参
      // this.$router.push(`/search/${this.keyword}?k=${this.keyword.toUpperCase()}`)//param传参+query传参
    
 //第三种:对象写法
      // this.$router.push({name:"search",params:{keyword:this.keyword},query:{k:this.keyword.toUpperCase()}})

路由组件中:

<h1>params参数-----{{ $route.params.keyword }}</h1>
<h1>query参数------{{ $route.query.k }}</h1> 

2.1.2通过props传参(小白,说的不对,请指正,谢谢)
在配置路由时:

{
      path:"/search/:keyword?",//params:路由传参,需要在路径中进行占位
      component:Search,
      meta:{
        showFooter:true
      },
      name:"search",
      props:true,//当 props 设置为 true 时,route.params 将被设置为组件的 props
    },

路由组件中

<h1>params参数-----{{ $route.params.keyword }}======{{ keyword }}</h1>
props:["keyword"]

添加链接描述
添加链接描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值