axios.get('http://localhost:3000/brands') //就是一個promise對象 .then(res=>{ // res 使用響應對象 包含了響應的數據 console.log(res.data) //響應的數據 }) .catch(err=>{ // err 錯誤對象 console.log(err.message) }) 有請求體 xios.post('http://localhost:3000/brands', { // 請求主體的數據 brandName: 'xxx', createTime: new Date() }).then(res => { console.log(res.data) }).catch(err => { console.log(err.message) })vue
// 其餘請求函數 // axios.get() // axios.post() // axios.put() // axios.delete()ios
09-表格案例-axios版-列表axios
data: { list: [] }, mounted () { // 視圖安裝完成 渲染完成 axios獲取全部品牌數據 axios.get('http://localhost:3000/brands') .then(res => { // 渲染列表 res.data 全部品牌數據 // 使用指令去渲染視圖 this.list = res.data }).catch(err => { //錯誤提示 alert('請求品牌數據失敗') }) }promise
{{item.id}} {{item.brandName}} {{item.createTime}} 刪除 10-表格案例-axios版-添加<button class="btn btn-primary" @click="add()">添加 data: { list: [], brandName: '' }, // methods 定義 add () { // 準備數據 const brand = { brandName: this.brandName, createTime: new Date() } // post 請求 axios.post('http://localhost:3000/brands', brand) .then(res => { // 處理成功時候 更新列表 this.getList() }) .catch(err => alert('添加品牌失敗')) //清空輸入框 this.brandName = '' }, 封裝getList函數 // methods 定義 getList () { // 視圖安裝完成 渲染完成 axios獲取全部品牌數據 axios.get('http://localhost:3000/brands') .then(res => { // 渲染列表 res.data 全部品牌數據 // 使用指令去渲染視圖 this.list = res.data }).catch(err => { //錯誤提示 alert('請求品牌數據失敗') }) }函數