<!DOCTYPE html>
<html lang="en">
<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="./lib/vue.js"></script>
<script src="./lib/vue-router.js"></script>
</head>
<body>
<div id="app">
<router-link to="/genmulu">根目录内容</router-link>
<router-view></router-view>
</div>
<template id="genmu">
<div>
<h2>这是根目录显示的内容</h2>
<router-link to="/genmulu/login">登陆</router-link> //这里面进行页面跳转的需要将前面的父级路由带上
<router-link to="/genmulu/resigest">注册</router-link>
</div>
</template>
<script>
// 1、定义两个组件
var genmulu = {
template: "#genmu"
}
var login = {
template: "<h2>登陆里面的子组件</h2>"
}
var zuche = {
template: "<h2>注册里面的子组件</h2>"
}
// 2、new一个路由规则
var routerobj = new VueRouter({
routes: [
{
path: '/genmulu',
component: genmulu,
children: [ //使用children 为数组,里面可以有路由规则对象,path后面不用带/,(/代表根目录,由于我们是路由嵌套,而是不是针对根目录的路径,所以children里面的路由规则对象不需要带/)
{ path: 'login', component: login },
{ path: 'resigest', component: zuche }
]
}
]
})
var vm = new Vue({
el: "#app",
data: {},
methods: {},
router: routerobj //挂载路由规则
})
</script>
</body>
</html>
代码有点问题还在处理中,有时间就在在敲一遍
来源:https://www.cnblogs.com/Progress-/p/12209158.html