<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="./lib/vue-2.4.0.js"></script> <script src="./lib/vue-resource-1.3.4.js"></script> <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css"> </head> <body> <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> </div> <script> // 如果我們通過全局配置了,請求的數據接口 根域名,則 ,在每次單獨發起 http 請求的時候,請求的 url 路徑,應該以相對路徑開頭,前面不能帶 / ,否則 不會啟用根路徑做拼接; Vue.http.options.root = 'http://vue.studyit.io/'; // 全局啟用 emulateJSON 選項 Vue.http.options.emulateJSON = true; // 創建 Vue 實例,得到 ViewModel var vm = new Vue({ el: '#app', data: { name: '', list: [ // 存放所有品牌列表的數組 ] }, created() { // 當 vm 實例 的 data 和 methods 初始化完畢后,vm實例會自動執行created 這個生命周期函數 this.getAllList() }, methods: { getAllList() { // 獲取所有的品牌列表 // 分析: // 1. 由於已經導入了 Vue-resource這個包,所以 ,可以直接通過 this.$http 來發起數據請求 // 2. 根據接口API文檔,知道,獲取列表的時候,應該發起一個 get 請求 // 3. this.$http.get('url').then(function(result){}) // 4. 當通過 then 指定回調函數之后,在回調函數中,可以拿到數據服務器返回的 result // 5. 先判斷 result.status 是否等於0,如果等於0,就成功了,可以 把 result.message 賦值給 this.list ; 如果不等於0,可以彈框提醒,獲取數據失敗! this.$http.get('api/getprodlist').then(result => { // 注意: 通過 $http 獲取到的數據,都在 result.body 中放着 var result = result.body if (result.status === 0) { // 成功了 this.list = result.message } else { // 失敗了 alert('獲取數據失敗!') } }) }, add() { // 添加品牌列表到后台服務器 // 分析: // 1. 聽過查看 數據API接口,發現,要發送一個 Post 請求, this.$http.post // 2. this.$http.post() 中接收三個參數: // 2.1 第一個參數: 要請求的URL地址 // 2.2 第二個參數: 要提交給服務器的數據 ,要以對象形式提交給服務器 { name: this.name } // 3.3 第三個參數: 是一個配置對象,要以哪種表單數據類型提交過去, { emulateJSON: true }, 以普通表單格式,將數據提交給服務器 application/x-www-form-urlencoded // 3. 在 post 方法中,使用 .then 來設置成功的回調函數,如果想要拿到成功的結果,需要 result.body /* this.$http.post('api/addproduct', { name: this.name }, { emulateJSON: true }).then(result => { if (result.body.status === 0) { // 成功了! // 添加完成后,只需要手動,再調用一下 getAllList 就能刷新品牌列表了 this.getAllList() // 清空 name this.name = '' } else { // 失敗了 alert('添加失敗!') } }) */ this.$http.post('api/addproduct', { name: this.name }).then(result => { if (result.body.status === 0) { // 成功了! // 添加完成后,只需要手動,再調用一下 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('刪除失敗!') } }) } } }); </script> </body> </html>