路由的注册
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<!--<router-link to="/">首页</router-link>-->
<!--<router-link to="/login">登录</router-link>-->
<router-link :to="{name: 'home'}">首页</router-link>
<router-link :to="{name: 'login'}">登录</router-link>
<router-link to="/xumingyang?age=77">用户页面</router-link>
<router-view></router-view>
</div>
<script>
let url = [
{
path: "/",
name: "home",
component: {
template: `<div>
<h1>这是首页</h1>
<h2>首页内容</h2>
</div>
`
}
},
{
path: "/login",
name: "login",
component: {
template: `
<div>
<h1>这是登录页面</h1>
</div>`
}
},
{
path: "/:username",
name: "user",
component: {
template: `<div>
<h1>这是用户页面</h1>
<p>用户名{{$route.params.username}}</p>
<p>年龄{{$route.query.age}}</p>
</div>`
}
}
];
let my_router = new VueRouter({
routes: url,
});
const app = new Vue({
el: "#app",
router: my_router,
mounted(){
// 路由的全部信息
console.log(this.$route);
console.log(this.$router)
console.log(this.$el)
}
})
</script>
</body>
</html>
子路由的注册
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<router-link :to="{name: 'home'}">首页</router-link>
<router-view></router-view>
</div>
<script>
let routes = [
{
path:"/",
name:"home",
redirect:"daien",
component:{
template:`<div>
<h1>这是首页</h1>
<router-link to="daien">戴恩</router-link>
<router-link to="zhangqiangqiang">张强强</router-link>
<router-view></router-view>
</div>`
},
children:[
{
path:"daien",
component:{
template:`<div><h1>戴恩</h1></div>`
}
},
{
path:"zhangqiangqiang",
component:{
template:`<div><h1>张强强</h1></div>`
}
}
]
}
];
let router = new VueRouter({
routes:routes
});
const app = new Vue({
el:"#app",
router:router,
})
</script>
</body>
</html>