Vue-router的传参方式和router使用技巧

vue传参方法一

1,路由配置vue

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

2,使用方法this

//   直接调用$router.push 实现携带参数的跳转
 this.$router.push({
//  这个id是一个变量,随即是什么值均可以
    path: /describe/${id}`,
 })

 3,获取方法(在describe页面)url

$route.params.id

使用以上方法能够拿到上个页面传过来的id值spa

vue传参方法二

 1,路由配置code

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

(这个地方默认配置就能够了,不用作任何的处理)component

2,使用方法router

this.$router.push({
       name: 'Describe',
       params: {
         id: id
      }
   })

父组件中:经过路由属性中的name来肯定匹配的路由,经过params来传递参数。对象

 3,获取方法(在describe页面)blog

$route.params.id

也用params获取就能够了;路由

vue传参方法三

 1,路由配置

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

(默认配置)

2,使用方法

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

(params换成了query)

 3,获取方法(在describe页面)

$route.query.id

(这个地方用query还获取id,和前面用的params获取的区别在于,用query获取的id值会在url中有显示,能够看到你传过来的值)

props传值方法

父组件

(table-data这个地方能够随便取名字,不是特定的值)

<div class="content">
//这个是一个普通组件,其中tabelData能够是变量,也能够是常量,和pageInfo同样样,这里打算传递两个值过去,其实也能够用对象的方式传过去都是能够的。
    <my-table :table-data="tableData" :page-info="pageInfo" id="myTable"></my-table>
</div>

子组件

props: ['tableData', 'pageInfo'],
data() {
    return {
        tData: this.tableData,
        page: this.pageInfo
    }
}

prop是单向绑定的,不该该在子组件内部改变prop。不过这里的props传过来的值会随之父组件的值的改变而改变,是动态改变的。

$route使用小技巧

1,$route.path

  • 类型: string

    字符串,对应当前路由的路径,老是解析为绝对路径,如 "/foo/bar"

2,$route.params

  • 类型: Object

  一个 key/value 对象,包含了动态片断和全匹配片断,若是没有路由参数,就是一个空对象。

3,$route.query

  • 类型: Object

    一个 key/value 对象,表示 URL 查询参数。例如,对于路径 /foo?user=1,则有 $route.query.user == 1,若是没有查询参数,则是个空对象。

4,$route.hash

  • 类型: string

    当前路由的 hash 值 (带 #) ,若是没有 hash 值,则为空字符串。

5,$route.fullPath

  • 类型: string

    完成解析后的 URL,包含查询参数和 hash 的完整路径。

相关文章
相关标签/搜索