环境
npm install -g json-server
npm install axios
新建db.json
json-server --watch db.json
获取
getList() {
this.$axios
.get("http://localhost:3000/posts")
.then(function (response) {
console.log("getList", response.data);
})
.catch(function (error) {
console.log(error);
});
},
新增
addData() {
let params = {
id: 2,
title: "aaa",
author: "bbb",
};
this.$axios
.post("http://localhost:3000/posts", params)
.then(function (response) {
console.log("getList", response);
})
.catch(function (error) {
console.log(error);
});
},
删除
deleteData() {
this.$axios
.delete("http://localhost:3000/posts/" + 2)
.then(function (response) {
console.log("getList", response);
})
.catch(function (error) {
console.log(error);
});
}
更新
updateData() {
let params = {
id: 5,
title: "555",
author: "444",
};
this.$axios
.put("http://localhost:3000/posts/" + 2, params)
.then(function (response) {
console.log("getList", response);
})
.catch(function (error) {
console.log(error);
});
}