一、路由
路由:vue-router 帶三方工具庫
創(chuàng)建單頁面應(yīng)用 spa (single page application)
通過不同的URL訪問不同的頁面
下載:
npm install vue
npm install vue-router
body部分:
<div id="itany">
<!-- 1.-->
<router-link to='/index'>首頁</router-link>
<router-link to='/detail'>詳情頁</router-link>
<router-link to='/about'>關(guān)于我們</router-link>
<!-- 盛放導(dǎo)航對(duì)應(yīng)的內(nèi)容-->
<router-view></router-view>
js部分:
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script>
// 2.創(chuàng)建組件
var Index={
template:`
<h1>這是首頁</h1>
`
}
var Detail={
template:`
<h1>這是詳情頁</h1>
`
}
var About={
template:`
<h1>這是關(guān)于我們</h1>
`
}
// 3.配置路由
const routes=[
{path:'/',component:Index},
{path:'/index',component:Index},
{path:'/detail',component:Detail},
{path:'/about',component:About}
]
// 4.創(chuàng)建一個(gè)路由實(shí)例
const router=new VueRouter({
routes:routes,
linkActiveClass:'active'
})
// 5.把路由實(shí)例掛載到vue實(shí)例上
new Vue({
el:"#itany",
router:router
})
</script>
二汽纤、路由的嵌套
body部分:
<div id="itany">
<router-link to='/index'>首頁</router-link>
<router-link to='/user'>用戶頁</router-link>
<router-view></router-view>
</div>
js部分:
<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'>這是注冊(cè)頁面</router-link></li>
<li><router-link to='/user/login'>這是登錄頁面</router-link></li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<h3>注冊(cè)頁面</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)建路由實(shí)例
const router=new VueRouter({
routes:routes
})
new Vue({
el:"#itany",
router:router
})
</script>
三上岗、路由的傳參
路由的傳參:
查詢字符串:
/user/regist?uname=jack&&upwd=123
接收:{{route.params}}
-->
body部分:
<div id="itany">
<router-link to='/index'>首頁</router-link>
<router-link to='/user'>用戶頁</router-link>
<router-view></router-view>
</div>
js部分:
<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?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>
`
}
// 字符串傳參 /user/regist?uname=jack&upwd=123
// 接收 {{$route.query}}
// rest傳參 /user/login/rose/456
// 配置路由:{path:'login/:userName/:password',component:Login}
// 接收:{{$route.params}}
var Regist={
template:`
<div>
<h3>注冊(cè)頁面</h3>
<a href="#">{{$route.query}}</a>
<ul>
<li>{{$route.query.uname}}</li>
<li>{{$route.query.upwd}}</li>
</ul>
</div>
`
}
var Login={
template:`
<div>
<h3>登錄頁面</h3>
<a href="#">{{$route.params}}</a>
<ul>
<li>{{$route.params.userName}}</li>
<li>{{$route.params.password}}</li>
</ul>
</div>
`
}
// 3.配置路由
const routes=[
{path:'/',component:Index},
{path:'/index',component:Index},
{
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,
linkActiveClass:'active'
})
new Vue({
el:"#itany",
router:router
})
</script>
效果圖:
四、vue中的ajax (axios)
vue中的ajax 插件
下載axios:
npm install axios
1.0 vue-resource
2.0 axios
安裝http-server
npm install http-server -g
使用http-server 開啟一個(gè)服務(wù)器
body部分:
<div id='app'>
<!--1.-->
<router-link to='/index'>首頁</router-link>
<router-link to='/detail'>詳情頁</router-link>
<!--盛放導(dǎo)航對(duì)應(yīng)的內(nèi)容-->
<router-view></router-view>
</div>
js部分:
<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>這是首頁</h1>
`
}
var Detail={
template:`
<div>
<h1>我是詳情頁</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>
json部分:
[
{
"num":1,
"pname":"apple",
"price":3,
"count":4,
"sub":12
},
{
"num":2,
"pname":"pear",
"price":4,
"count":5,
"sub":20
},
{
"num":3,
"pname":"orange",
"price":5,
"count":6,
"sub":30
}
]
鏈接網(wǎng)址:
http://192.168.1.108:8080
http://127.0.0.1:8080
效果圖: