JSFiddle - React, Tailwind, and code Playground
by simati
HTML
<script src="https://cdn.jsdelivr.net/lodash/4.15.0/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/vue/2.0.0-rc.4/vue.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css">
<script src="https://cdn.jsdelivr.net/vue.router/2.0.0-rc.4/vue-router.min.js"></script>
<div id="app"></div>
JavaScript
Vue.use(VueRouter)
// A route component can also contain <router-view> to render
// nested children route components
const Parent = {
template: `
<div class="parent">
<h2>Parent</h2>
<router-view class="child"></router-view>
</div>
`
}
const Default = { template: '<div>default</div>' }
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Baz = { template: '<div>baz</div>' }
const router = new VueRouter({
routes: [
{ path: '/', redirect: '/parent' },
{ path: '/parent', component: Parent,
children: [
// an empty path will be treated as the default, e.g.
// components rendered at /parent: Root -> Parent -> Default
{ path: '', component: Default },
// components rendered at /parent/foo: Root -> Parent -> Foo
{ path: 'foo', component: Foo },
// components rendered at /parent/bar: Root -> Parent -> Bar
{ path: 'bar', component: Bar },
// NOTE absolute path here!
// this allows you to leverage the component nesting without being
// limited to the nested URL.
// components rendered at /baz: Root -> Parent -> Baz
{ path: '/baz', component: Baz }
]
}
]
})
new Vue({
router,
template: `
<div id="app">
<h1>Nested Routes</h1>
<ul>
<li><router-link to="/parent" @click="parentClicked">/parent</router-link></li>
<li><router-link to="/parent/foo">/parent/foo</router-link></li>
<li><router-link to="/parent/bar">/parent/bar</router-link></li>
<li><router-link to="/baz">/baz</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
`,
methods: {
parentClicked(){
console.log('parentClicked')
}
}
}).$mount('#app')