天天看點

路由參數-params方式和query方式

路由傳參,這裡的參數名隻是一個描述符,寫什麼都可以

params方式傳遞路由參數:

$route.params傳回的是params方式傳遞的路由參數

eg:path:’/goods/:id/:title’

$router是路由總資訊對象

$route是目前路由資訊對象

:to="’/goods/’+(index+1)+’/’+item"

定義id,title

id:0,title:’’

let {id,title} =this.$route.params

this.id = id

this.title = title

根據id查詢相關資料

axios.get(‘aasdfghjk’,{id:id})

this.show = this.goodsList.find(r=>r.id===parseInt(id))

路由傳參還可以配合props使用,通過props解耦

props:true

可以不用定義屬性,用props去接一下

這裡props裡面定義的兩個參數,可以去接收路由傳過來的兩個參數

props:[“id”,“title”]

this.show = this.goodsList.find(r=>r.id===parseInt(this.id))

query方式傳遞路由參數:

好處:不需要動路由

$route.query傳回的是query方式傳遞的路由參數

path:‘goods’

問号後面跟參數,多個參數用&拼接

:to="’/goods/?id=’+(index+1)+’&title’+item"

id:0,title:’’

let {id,title} = this.$route.query

this.id = id

this.title = title

根據id查詢相關資料

this.show = this.goodsList.find(r=>r.id===parseInt(this.id))

vue