Passing Props to Route Component
Passing Props to Route Component: this means to pass props by router.
There are two options to receive props:
first one is inside template: $route.params.id
second one is as props inside vue component : props: ['id']
Example:
product.js for router file
As you see above code, detail component contains props id:
How to pass props from route configuration file.
We have path:'detail/:id' and props:{ detail: true}. this means that we pass id as props from template to detail component.
How we pass id props from template is here.
Props can be passed as function:
App.vue component file
There are two options to receive props:
first one is inside template: $route.params.id
second one is as props inside vue component : props: ['id']
Example:
product.js for router file
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const navBar = {
template: `<div>
<ul>
<li><router-link v-bind:to="{ path:'list' }">Product all</router-link></li>
</ul>
</div>`
}
const setting = {
template: `
<div>
<navBar />
<router-view />
<router-view name='status' />
<router-view name='detail' />
</div>`,
components:{
navBar
}
}
const status = {
template: `<div> product status </div>`
}
const detail = {
props:['id'],
template: `<div> product detail Id: {{ id }} </div>`
}
const products = {
template:`<div>
<li v-for="product in products" v-bind:key="product.id">
id: {{product.id}}, name: {{product.name}}, status: {{ product.status}}
<br/>
<router-link v-bind:to="{ name: 'detail', params:{id:product.id }}">Product detail</router-link>
</li>
</div>`,
watch:{
'$route' (to, from){
console.log(from)
}
},
data () {
return {
products:[
{id:1, name:'motobike', status:'old'},
{id:2, name:'fan', status:'new'},
{id:3, name:'watch', status:'new'},
{id:4, name:'glasses', status:'old'}
]
}
},
}
const routes = [
{ path: '/',
name: 'setting',
component: setting,
children:[
{
path:'products',
name:'products',
alias: 'list',
components:{
default:products,
status:status
}
},
{
path:'detail/:id',
name:'detail',
components:{
detail:detail
},
props: { detail : true }
}
]
}
]
const router = new VueRouter({
mode:'history',
routes // short for `routes: routes`
})
export { router }
As you see above code, detail component contains props id:
const detail = {
props:['id'],
template: `<div> product detail Id: {{ id }} </div>`
}
How to pass props from route configuration file.
{
path:'detail/:id',
name:'detail',
components:{
detail:detail
},
props: { detail : true }
}
How we pass id props from template is here.
<router-link v-bind:to="{ name: 'detail', params:{id:product.id }}">Product detail</router-link>
Props can be passed as function:
props: (route) => ({ id: route.query.id})
App.vue component file
<template>
<div id="app">
<h2>Products</h2><br/>
<router-view></router-view>
</div>
</template>
<script>
import { router } from './routes/product';
export default {
name: 'app',
router,
methods:{
goProduct( productId = null ) {
if(productId) {
this.$router.push({ name:'product', params:{ id:productId } })
}
}
}
}
</script>
Comments
Post a Comment