多层嵌套:
<Meta charset="UTF-8">
Document
<template id="home">
<h3>我是主页</h3>
<div>
<a v-link="{path:'/home/login'}">登录</a>
<a v-link="{path:'/home/reg'}">注册</a>
</div>
<div>
<router-view></router-view>
</div>
</template>
<template id="news">
<h3>我是新闻</h3>
</template>
<script>
<span style="color: #008000;">//</span><span style="color: #008000;">1. 准备一个根组件</span>
<span style="color: #0000ff;">var</span> App=<span style="color: #000000;">Vue.extend();
</span><span style="color: #008000;">//</span><span style="color: #008000;">2. Home News组件都准备</span>
<span style="color: #0000ff;">var</span> Home=<span style="color: #000000;">Vue.extend({
template:</span>'#home'<span style="color: #000000;">
});
</span><span style="color: #0000ff;">var</span> News=<span style="color: #000000;">Vue.extend({
template:</span>'#news'<span style="color: #000000;">
});
</span><span style="color: #008000;">//</span><span style="color: #008000;">3. 准备路由</span>
<span style="color: #0000ff;">var</span> router=<span style="color: #0000ff;">new</span><span style="color: #000000;"> VueRouter();
</span><span style="color: #008000;">//</span><span style="color: #008000;">4. 关联</span>
<span style="color: #000000;"> router.map({
'home'<span style="color: #000000;">:{
component:Home,subRoutes:{
'login'<span style="color: #000000;">:{
component:{
template:'我是登录信息'<span style="color: #000000;">
}
},'reg'<span style="color: #000000;">:{
component:{
template:'我是注册信息'<span style="color: #000000;">
}
}
}
},'news'<span style="color: #000000;">:{
component:News
}
});
</span><span style="color: #008000;">//</span><span style="color: #008000;">5. 启动路由</span>
router.start(App,'#<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>'<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">6. <a href="https://www.jb51.cc/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a></span>
<span style="color: #000000;"> router.redirect({
'/':'home'<span style="color: #000000;">
});