json-server的簡單使用:
1.全局安裝:cnpm install json-server -g
2.創建文件夾/文件 server/data.json
在data.json文件中手動添加一些數據:例如:
{ "list":[ { "name":"蘋果", "price":22 }, { "name":"香蕉", "price":30 }, { "name":"雪梨", "price":60 } ] }
3.在server文件夾內打開cmd命令窗口,運行json-server data.json;每當data.json文件手動改變,都得重新運行
4.增刪改查:
get 查,post 增,delete 刪,patch 修改部分數據,put 全部數據替換
例如:
axios({ method:"get", url:"localhost:3000/list" }).then((data)=>{console.log(data)}) axios({ method:"post", url:"http://localhost:3000/list", data:{name:"蘋果",price:"22"} }) axios({ method:"patch", url:"http://localhost:3000/list/1", data:{name:"臍橙"} })
其中常用的url的形式有:
全部數據: localhost:3000/list
指定id: localhost:3000/list/1
指定條件: localhost:3000/list?name=李四&name=張三
模糊查詢: localhost:3000/list?q=張