1,vue-router
vue的核心插件
vue-router.js
npm install vue-router
根據(jù)不同的url訪問不同的頁面
創(chuàng)建單頁面SPA(SINGLE PAGE APPLICATION)應(yīng)用
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
.active{
color:red;
}
</style>
<body>
<a></a>
<div id='app'>
<!--1.-->
<router-link to='/home'>首頁</router-link>
<router-link to='/user'>用戶頁</router-link>
<!-- 盛放鏈接對應(yīng)的內(nèi)容-->
<router-view></router-view>
</div>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script>
//2.創(chuàng)建組件
var Home={
template:`
<h1>這是首頁</h1>
`
}
var User={
template:`
<h1>這是用戶頁</h1>
`
}
//3.配置路由
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{path:'/user',component:User}
]
//4.創(chuàng)建路由實例
const router=new VueRouter({
routes:routes
})
//5.路由實例掛載到vue實例上
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
運行結(jié)果:
[圖片上傳中...(360截圖20180924144924144.jpg-e4b617-1537771791411-0)]

360截圖20180924144924144.jpg
路由的嵌套:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='app'>
<router-link to='/index'>首頁</router-link>
<router-link to='/user'>用戶頁</router-link>
<router-view></router-view>
</div>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script>
//2.創(chuàng)建組件
var Index={
template:`
<h1>這是首頁</h1>
`
}
var User={
template:`
<div>
<h1>這是用戶頁</h1>
<ul>
<li>
<router-link to='/user/regist'>注冊</router-link>
</li>
<li>
<router-link to='/user/login'>登錄</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<h3>這是注冊頁</h3>
`
}
var Login={
template:`
<h3>這是登錄頁</h3>
`
}
//3.配置路由
const routes=[
{path:'/',component:Index},
{path:'/index',component:Index},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login',component:Login}
]
}
]
//4.創(chuàng)建路由實例
const router=new VueRouter({
routes:routes
})
//5.把路由實例掛載到vue實例上
new Vue({
el:'#app',
router:router//注冊路由
})
</script>
</body>
</html>
運行結(jié)果:
360截圖20180924144251800.jpg