Named Views

Named Views: instead of nested multi-views to render at the same time, named views can help to simplify not to nest.

Example:
product.js for route 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:'products' }">Product all</router-link></li>
<li><router-link v-bind:to="{ path:'detail' }">Product detail</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 = {
template: `<div> product detail </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}}
</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',
components:{
default:products,
status:status
}
},
{
path:'detail',
components:{
detail:detail
}
}
]
}
]

const router = new VueRouter({
mode:'history',
routes // short for `routes: routes`
})
export { router }

here is the place to note
<router-view />
<router-view name='status' />
<router-view name='detail' />
first line is to render product component: default:products.
second line is to render status component: status:status
third line is to render detail component: detail:detail.

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
}
</script>

You may click on a button to see list of product with product status text.

final result :

Comments

Popular posts from this blog

Vuex mapMutations

VueX Actions

VueX Modules