vue三種傳參方式

<p @click="btn(id)"></p>

  

第一種:經過路由中的name屬性來肯定匹配的路由(實際開發中不推薦)javascript

  子組件經過$route.name接收參數java

   {
      path: '/news',
      name: 'News',
      component:News
    }, 

  子組件接收:this

<p>{{$route.name}}</p>

  第二種:經過router-link中的to屬性url

  對應路由配置:component

  {
     path: '/describe',
     name: 'Describe',
     component: Describe
   }

  經過路由中的name屬性來肯定匹配的路由,經過params來傳遞參數(params是一個對象,裏面是key:value的形式):router

<router-link :to="{name:'Describe',params:{username:'name',id:'id'}}">goHome</router-link> |

  子組件接收:對象

this.$route.params.username
this.$route.params.id
或者插值形式直接展現在頁面 <p>{{$route.params.username}}-{{$route.params.id}}</p>

  

第二種:經過url傳參blog

  {
     path: '/describe:/newsID(\\d+)/:newsTitle',          這裏的正則是爲了限制id只能傳遞數字
     name: 'Describe',
     component: Describe
   }

  路由參數:ip

<router-link to="/describe/19/hi"></router-link>

  子組件接受參數($route.params):路由

<p>{{$route.params.newsId}}-{{$route.params.newsTitle}}</p>

第三種:使用path來匹配路由組件,而後經過query來傳遞參數。這種狀況下參數會在url後面顯示?id=?

      this.$router.push({
          path: '/describe',
          query: {
            id: id
          }
        })

  對應路由配置:

  {
     path: '/describe',
     name: 'Describe',
     component: Describe
   }

  子組件接收參數:

this.$route.query.id
相關文章
相關標籤/搜索