路由当中的嵌套,需要用到
router对象
身上的children
属性,这个属性是一个数组,里面传入子组件的路由规则
例:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="js/vue.js"></script>
<script src="js/vue-router-3.0.1.js"></script>
</head>
<body>
<div id="app">
<router-link to="/account">account</router-link>
<router-view></router-view>
</div>
<template id="tmpl">
<div>
<h1>这是Account组件</h1>
<router-link to='/account/login'>登陆</router-link>
<router-link to='/account/register'>注册</router-link>
<!-- 子组件的模板占位符 -->
<router-view></router-view>
</div>
</template>
<script>
var account = {
template: '#tmpl'
}
var login = {
template: '<h3>登陆</h3>'
}
var register = {
template: '<h3>注册</h3>'
}
var router = new VueRouter({
routes: [
{
path: '/account',
component: account,
// 子组件的路由匹配放在children属性当中,注意,子路由的路径不要带 /
children: [
{path: 'login', component: login},
{path: 'register', component: register}
]
}
]
})
var vm = new Vue({
el: '#app',
data: {},
methods: {},
router
});
</script>
</body>
</html>