开发版本:axios.js
生产版本:axios.min.js
{
"posts" : [
{
"title" : "title1",
"anthor" : "anthor1",
"id" : 1
},
{
"title" : "title2",
"anthor" : "anthor2",
"id" : 2
}
]
}
<script src="../axios.js"></script>
<script src="../vue.js"></script>
<div id="app1">
<button @click="search">get</button>
<button @click="add">add</button>
<ol>
<li v-for="item in postList" :key="item.id">
{{item.title}} {{item.author}}
<button @click="remove(item.id)">delete</button>
</li>
</ol>
</div>
<script>
// 设置url
axios.defaults.baseURL = "http://localhost:3000";
new Vue({
el:'#app1',
data : {
postList : []
},
methods : {
search(){
axios.get('/posts')
.then(res => {
console.log(res);// 请求成功
this.postList = res.data;
}).catch(reason => {
console.log(reason);// 请求失败
});
},
add(){
axios.post('/posts',{
title : "add",
author : "zhangsan"
}).then(res => {
console.log(res);
}).catch(reason => {
console.log(reason);
});
},
// 异步写法
async remove(id){
try{
await axios.delete('/posts/' + id);
}catch (e) {
console.log(e);
}
}
}
});
</script>
原文地址:https://blog.csdn.net/h_e_l_l_o_______/article/details/134565509
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_1771.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!