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
相关文章
相关标签/搜索