案例改進(jìn)
vue-resource全局配置:
Vue.http.options.root = 'http://vue.studyit.io/';
全局啟用 emulateJSON 選項(xiàng)
Vue.http.options.emulateJSON = true;
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加品牌</h3>
</div>
<div class="panel-body form-inline">
<label>
Name:
<input type="text" v-model="name" class="form-control">
</label>
<input type="button" value="添加" @click="add" class="btn btn-primary">
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.ctime}}</td>
<td>
<a href="" @click.prevent="del(item.id)">刪除</a>
</td>
</tr>
</tbody>
</table>
// 如果我們通過全局配置了,請求的數(shù)據(jù)接口 根域名,則 ,在每次單獨(dú)發(fā)起 http 請求的時(shí)候,請求的 url 路徑,應(yīng)該以相對路徑開頭,前面不能帶 / ,否則 不會啟用根路徑做拼接;
Vue.http.options.root = 'http://vue.studyit.io/';
// 全局啟用 emulateJSON 選項(xiàng)
Vue.http.options.emulateJSON = true;
// 創(chuàng)建 Vue 實(shí)例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {
name: '',
list: [ // 存放所有品牌列表的數(shù)組
]
},
created() { // 當(dāng) vm 實(shí)例 的 data 和 methods 初始化完畢后,vm實(shí)例會自動執(zhí)行created 這個(gè)生命周期函數(shù)
this.getAllList()
},
methods: {
getAllList() { // 獲取所有的品牌列表
// 分析:
// 1. 由于已經(jīng)導(dǎo)入了 Vue-resource這個(gè)包,所以 ,可以直接通過 this.$http 來發(fā)起數(shù)據(jù)請求
// 2. 根據(jù)接口API文檔,知道,獲取列表的時(shí)候,應(yīng)該發(fā)起一個(gè) get 請求
// 3. this.$http.get('url').then(function(result){})
// 4. 當(dāng)通過 then 指定回調(diào)函數(shù)之后,在回調(diào)函數(shù)中,可以拿到數(shù)據(jù)服務(wù)器返回的 result
// 5. 先判斷 result.status 是否等于0,如果等于0,就成功了,可以 把 result.message 賦值給 this.list ; 如果不等于0,可以彈框提醒,獲取數(shù)據(jù)失?。?
this.$http.get('api/getprodlist').then(result => {
// 注意: 通過 $http 獲取到的數(shù)據(jù),都在 result.body 中放著
var result = result.body
if (result.status === 0) {
// 成功了
this.list = result.message
} else {
// 失敗了
alert('獲取數(shù)據(jù)失敗!')
}
})
},
add() { // 添加品牌列表到后臺服務(wù)器
// 分析:
// 1. 聽過查看 數(shù)據(jù)API接口,發(fā)現(xiàn),要發(fā)送一個(gè) Post 請求, this.$http.post
// 2. this.$http.post() 中接收三個(gè)參數(shù):
// 2.1 第一個(gè)參數(shù): 要請求的URL地址
// 2.2 第二個(gè)參數(shù): 要提交給服務(wù)器的數(shù)據(jù) ,要以對象形式提交給服務(wù)器 { name: this.name }
// 3.3 第三個(gè)參數(shù): 是一個(gè)配置對象,要以哪種表單數(shù)據(jù)類型提交過去, { emulateJSON: true }, 以普通表單格式,將數(shù)據(jù)提交給服務(wù)器 application/x-www-form-urlencoded
// 3. 在 post 方法中,使用 .then 來設(shè)置成功的回調(diào)函數(shù),如果想要拿到成功的結(jié)果,需要 result.body
/* this.$http.post('api/addproduct', { name: this.name }, { emulateJSON: true }).then(result => {
if (result.body.status === 0) {
// 成功了!
// 添加完成后,只需要手動,再調(diào)用一下 getAllList 就能刷新品牌列表了
this.getAllList()
// 清空 name
this.name = ''
} else {
// 失敗了
alert('添加失敗!')
}
}) */
this.$http.post('api/addproduct', { name: this.name }).then(result => {
if (result.body.status === 0) {
// 成功了!
// 添加完成后,只需要手動,再調(diào)用一下 getAllList 就能刷新品牌列表了
this.getAllList()
// 清空 name
this.name = ''
} else {
// 失敗了
alert('添加失?。?)
}
})
},
del(id) { // 刪除品牌
this.$http.get('api/delproduct/' + id).then(result => {
if (result.body.status === 0) {
// 刪除成功
this.getAllList()
} else {
alert('刪除失??!')
}
})
}
}
});