<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VueRouter</title>
<script src="js/vue.js"></script>
<script type="text/javascript" src="js/vue-router.js"></script>javascript
<template id="users">
<div>
<div>I am the users page !</div>
<hr>
<router-link to='/users/login'>Login</router-link>
<router-link to="/users/regist">Regist</router-link>
<router-view>Login and Regist areas !</router-view>
</div>
</template>html
<template id="detail">
<div>
<div>I am the detail page !</div>
{{$route.params}}
<br>
{{$route.path}}
<br>
{{$route.query}}
</div>
</template>vue
<template id="news">
<div>
<div>I am the news page !</div>
<hr>
<router-link to="/news/detail/001">News 001</router-link>
<router-link to="/news/detail/002">News 002</router-link>
<router-view></router-view>
</div>
</template>
<template id="home">
<div>
<div>I am the main page !</div>
<br>
<input type="button" value="ToUserPage" @click="push">
<input type="button" value="ToDetailPage" @click="replace">
</div>
</template>java
<script type="text/javascript">
window.onload=function(){
/*Definite component for routers*/
const home={
template:'#home',
methods:{
push(){
router.push({path:'/users'});
},
replace(){
router.replace({path:'/news/detail/005'})
}
}
};
const news={
template:'#news'
};
const users={
template:'#users'
};
const login={
template:'<div>I am the login page !</div>'
};
const regist={
template:'<div>I am the regist page !</div>'
};
const detail={
template:'#detail'
};vue-router
//Definite routerapp
/*位置一:寫在這裏是就對了,也就是:組件化
*寫在const router=new VueRouter({routes});路由定義的上面,就位置而言哈,別想多了,哈哈,,component
*/router
const routes=[{path:'/home',component:home},htm
{
path:'/news',
component:news,
children:[
{path:'detail/:aid',component:detail}]
},
{
path:'/users',
component:users,
children:[
{path:'login',component:login},
{path:'regist',component:regist}]
},
{path:'/',redirect:'/home'}//default contents of show
];
const router=new VueRouter({routes});
new Vue({
el:'#app',
/*'router':router*/
router //registe router
});
/*位置二:寫在這裏是就玩不下去了,也就是:
*寫在const router=new VueRouter({routes});路由定義的下面,個人這個當心髒啊,上看下看,左看右看,就是看不出是哪裏錯了,內心這個憋屈啊,至因而個什麼原理我就不懂了,初學者啊,之後再回來探個究竟吧,
*/
/*const routes=[{path:'/home',component:home},
{
path:'/news',
component:news,
children:[
{path:'detail/:aid',component:detail}]
},
{
path:'/users',
component:users,
children:[
{path:'login',component:login},
{path:'regist',component:regist}]
},
{path:'/',redirect:'/home'}//default contents of show
];*/
}
</script>
</head>
<body>
<div id="app">
<!--Use router-->
<router-link to="/home">HOME</router-link>
<router-link to="/news">NEWS</router-link>
<router-link to="/users">USERS</router-link>
<router-view>This is contents</router-view>
</div>
</body>
</html>
再順便總結下Vue中路由的幾個關鍵點吧:
1:須要引入<script type="text/javascript" src="js/vue-router.js"></script>,都組件化開發了,Vue中是沒有vue-router的,已經把它拎出來了,
2:須要指明路由存放的位置<router-view>This is contents</router-view>
3:路由的定義:const router=new VueRouter({routes});當把routes(const routes=[])拎出去寫的時候要把它寫在定義語句的前面
4: 路由中用到的一些方法:$route.params()/$route.path()/$route.query()/router.push({pach:'路徑'})/router.replace({pach:'路徑'})/router.go(1/-1):跳轉到前一頁/後一頁