1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/*
.router-link-active{
color:red;
}
*/
.active{
color:red;
}
</style>
</head>
<body>
<a href=""></a>
<div id='app'>
<!--1.-->
<router-link to='/index'>首頁(yè)</router-link>
<router-link to='/detail'>詳情頁(yè)</router-link>
<!--盛放導(dǎo)航對(duì)應(yīng)的內(nèi)容-->
<router-view></router-view>
</div>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script src='js/axios.js'></script>
<script>
//2. 創(chuàng)建組件
var Index={
template:`
<h1>這是首頁(yè)</h1>
`
}
var Detail={
template:`
<div>
<h1>我是詳情頁(yè)</h1>
<table border=1 cellspacing=0>
<thead>
<tr>
<td>編號(hào)</td>
<td>品名</td>
<td>單價(jià)</td>
<td>數(shù)量</td>
<td>小計(jì)</td>
</tr>
</thead>
<tbody>
<tr v-for="value in fruList">
<td>{{value.num}}</td>
<td>{{value.pname}}</td>
<td>{{value.price}}</td>
<td>{{value.count}}</td>
<td>{{value.sub}}</td>
</tr>
</tbody>
</table>
</div>
`,
data:function(){
return{
fruList:null
}
},
mounted:function(){
var self=this;
axios({
method:'get',//發(fā)送數(shù)據(jù)的方式
url:'fruit.json'
}).then(function(resp){//請(qǐng)求成功
// console.log(resp)
console.log(resp.data)
self.fruList=resp.data
}).catch(function(err){//請(qǐng)求失敗
console.log(err)
})
}
}
//3.配置路由
const routes=[
{path:'/',component:Index},
{path:'/index',component:Index},
{path:'/detail',component:Detail}
]
//4.創(chuàng)建一個(gè)路由實(shí)例
const router=new VueRouter({
routes:routes,
linkActiveClass:"active"
})
//5.把路由實(shí)例掛在到vue實(shí)例上
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!--
路由的傳參:
查詢字符串:
/user/regist?uname=jack&&upwd=123
接收:{{$route.query}}
rest風(fēng)格傳參
/user/login/rose/456
接收: {{$route.params}}
-->
</body>
</html>
3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<a href=""></a>
<div id='app'>
<!--1.-->
<router-link to='/index'>首頁(yè)</router-link>
<router-link to='/detail'>詳情頁(yè)</router-link>
<!--盛放導(dǎo)航對(duì)應(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 Index={
template:`
<h1>這是首頁(yè)</h1>
`
}
var Detail={
template:`
<h1>我是詳情頁(yè)</h1>
`
}
//3.配置路由
const routes=[
{path:'/index',component:Index},
{path:'/detail',component:Detail}
]
//4.創(chuàng)建一個(gè)路由實(shí)例
const router=new VueRouter({
routes:routes
})
//5.把路由實(shí)例掛在到vue實(shí)例上
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> /* .router-link-active{ color:red; } */ .active{ color:red; } </style> </head> <body> <a href=""></a> <div id='app'> <!--1.--> <router-link to='/index'>首頁(yè)</router-link> <router-link to='/detail'>詳情頁(yè)</router-link> <!--盛放導(dǎo)航對(duì)應(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 Index={ template:` <h1>這是首頁(yè)</h1> ` } var Detail={ template:` <h1>我是詳情頁(yè)</h1> ` } //3.配置路由 const routes=[ {path:'/',component:Index}, {path:'/index',component:Index}, {path:'/detail',component:Detail} ] //4.創(chuàng)建一個(gè)路由實(shí)例 const router=new VueRouter({ routes:routes, linkActiveClass:"active" }) //5.把路由實(shí)例掛在到vue實(shí)例上 new Vue({ el:'#app', router:router }) </script> </body> </html>
5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='app'>
<router-link to='/home'>首頁(yè)</router-link>
<router-link to='/user'>用戶頁(yè)</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 Home={
template:`
<h1>這是首頁(yè)</h1>
`
}
var User={
template:`
<div>
<h1>這是用戶頁(yè)</h1>
<ul>
<li>
<router-link to='/user/regist?uname=jack&&upwd=123'>注冊(cè)</router-link>
</li>
<li>
<router-link to='/user/login/rose/456'>登錄</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<div>
<h3>這是注冊(cè)頁(yè)</h3>
<a href='#'>{{$route.query}}</a>
<ul>
<li>{{$route.query.uname}}</li>
<li>{{$route.query.upwd}}</li>
</ul>
</div>
`
}
var Login={
template:`
<div>
<h3>這是登錄頁(yè)</h3>
<a href="">{{$route.params}}</a>
<ul>
<li>{{$route.params.userName}}</li>
<li>{{$route.params.password}}</li>
</ul>
</div>
`
}
//3.配置路由
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login/:userName/:password',component:Login}
]
}
]
//4.創(chuàng)建路由實(shí)例
const router=new VueRouter({
routes:routes
})
//5.
new Vue({
el:'#app',
router:router//注冊(cè)路由
})
</script>
</body>
</html>
6
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='app'>
<router-link to='/home'>首頁(yè)</router-link>
<router-link to='/user'>用戶頁(yè)</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 Home={
template:`
<h1>這是首頁(yè)</h1>
`
}
var User={
template:`
<div>
<h1>這是用戶頁(yè)</h1>
<ul>
<li>
<router-link to='/user/regist'>注冊(cè)</router-link>
</li>
<li>
<router-link to='/user/login'>登錄</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<h3>這是注冊(cè)頁(yè)</h3>
`
}
var Login={
template:`
<h3>這是登錄頁(yè)</h3>
`
}
//3.配置路由
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login',component:Login}
]
}
]
//4.創(chuàng)建路由實(shí)例
const router=new VueRouter({
routes:routes
})
//5.
new Vue({
el:'#app',
router:router//注冊(cè)路由
})
</script>
</body>
</html>
7
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!--
路由:vue-router 帶三方工具庫(kù)
創(chuàng)建單頁(yè)面應(yīng)用 spa (single page application)
通過(guò)不同的URL訪問(wèn)不同的頁(yè)面
下載:
npm install vue
npm install vue-router
axios:
vue中的ajax 插件
下載axios:
npm install axios
1.0 vue-resource
2.0 axios
安裝http-server
npm install http-server -g
使用http-server 開(kāi)啟一個(gè)服務(wù)器
-->
</body>
</html>