vue-router 參數傳遞的方式前端
Parma傳參
貼代碼:
/router/index.vuevue
export default new Router({ routes: [ { path: '/', name: 'Home', component: Home }, { path: '/work', name: 'Work', component: Work } ] })
組件Works傳遞一個work的id到組件Work
/components/Home/Comtent/Works.vuevue-router
// 觸發它傳遞一個對象到組件Work getIt (id) { this.$router.push({ path: '/work', name: 'Work', params: { id: id } }) }
/components/Work/Index.vuethis
<template> <div class="work"> work: {{id}} </div> </template> <script> export default { name: 'Work', data () { return { id: this.$route.params.id //拿到id } } } </script>
運行截圖:
url
query傳參
將上面的parmas改成query便可,即:spa
// 傳入 this.$router.push({ path: '/work', name: 'Work', query: { id: id } }) ... ... this.$route.query.id // 獲取
parmas與query的區別code
總結: 這兩種參數的傳遞方式,各有各的用途,具體的還要本身親手試一試才知道,前端這個領域,仍是要多多親自動手實踐。component