vue的核心插件
1,根據(jù)不同的url訪問不同的頁面
2青团, 創(chuàng)建單頁面SPA(SINGLE PAGE APPLICATION)應用
1,路由的跳轉(zhuǎn):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.router-link-exact-active {
color: red;
}
</style>
</head>
<body>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<div id="box">
<router-link to='/home'>首頁</router-link>
<router-link to='/home1'>首頁1</router-link>
<router-view></router-view>
</div>
<script>
var Home = {
template: `
<h1>home</h1>
`
}
var Home1 = {
template: `
<div>
<h1>home1</h1>
<ul>
<li>
<router-link to='/Home1/one'>登錄</router-link>
</li>
<li>
<router-link to='/Home1/two'>注冊</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var One = {
template: `
<h3>登錄</h3>
`
}
var Two = {
template: `
<h3>注冊</h3>
`
}
const routes = [{
path: '/',
component: Home
},
{
path: '/home',
component: Home
},
{
path: '/home1',
component: Home1,
children: [{
path: 'one',
component: One
},
{
path: 'two',
component: Two
}
]
}
]
const router = new VueRouter({
routes: routes,
})
new Vue({
el: '#box',
router: router
})
</script>
</body>
</html>
效果圖:
路由的嵌套:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<div id="box">
<router-link to='/home'>首頁</router-link>
<router-link to='/home1'>首頁1</router-link>
<router-view></router-view>
</div>
</body>
<script>
var Home = {
template: `
<h1>home</h1>
`
}
var Home1 = {
template: `
<div>
<h1>home1</h1>
<ul>
<li>
<router-link to='/home1/one'>登錄</router-link>
</li>
<li>
<router-link to='/Home1/two'>注冊</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var One = {
template: `
<h2>這是登錄<h2>
`
}
var Two = {
template: `
<h2>這是注冊</h2>
`
}
const routes = [
// components
{
path: '/',
component: Home
}
, {
path: '/home',
component: Home
}, {
path: '/home1',
component: Home1,
children:[
{path:'one',component:One},
{path:'two',component:Two},
]
}
]
const router = new VueRouter({
routes: routes
})
new Vue({
el: '#box',
router: router
})
</script>
</html>
效果圖: