axios:
vue ajax 前端頁面和后臺(tái)數(shù)據(jù)進(jìn)行交互 json
vue 庫
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='app'>
<router-link to='/home'>首頁</router-link>
<router-link to='/detail'>詳情頁</router-link>
<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>
var Home={
template:`
<h1>這是首頁內(nèi)容</h1>
}
var Detail={
template:`
<div>
<h1>這是詳情頁內(nèi)容</h1>
<table border=1 cellspacing=0>
<thead>
<tr>
<th>編號</th>
<th>品名</th>
<th>單價(jià)</th>
<th>數(shù)量</th>
<th>小計(jì)</th>
</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({
//發(fā)送數(shù)據(jù)的方式
method:'get',
url:'fruit.json'
//請求成功
}).then(function(resp)
console.log(resp.data)
self.fruList=resp.data
請求失敗
}).catch(function(err){
})
}
}
3.配置路由
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{path:'/detail',component:Detail}
]
const router=new VueRouter({ routes:routes }) 5. new Vue({ el:"#app", router:router }) </script> </body> </html>
下載:
npm install axios
訪問頁面:
網(wǎng)址:127.0.0.1:8080
安裝http-server:
npm install http-server -g