forked from jigneshbhimani/VueJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23DeleteRecordWithAPI.txt
81 lines (75 loc) · 2.29 KB
/
23DeleteRecordWithAPI.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
44.Delete Record with API:
- Fetch all data for list
- Show all record with table
- Make function for delete record
- Delete record on button click
- Test API result
- Make a new component User.vue in components folder.
<template>
<div>
<table border="1px">
<tr>
<td>Id</td>
<td>Title</td>
<td>Author</td>
<td>Action</td>
</tr>
<tr v-for="user in users" v-bind:key="user.id">
<td>{{user.id}}</td>
<td>{{user.title}}</td>
<td>{{user.author}}</td>
<td><Button v-on:click="deleteUser(user.id)">Delete</Button></td>
</tr>
</table>
</div>
</template>
<script>
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
export default {
name: 'User',
data()
{
return{users:null}
},
methods:
{
getUsers()
{
this.axios.get('http://localhost:3000/posts').then((result)=>{
console.warn(result)
this.users = result.data
})
},
deleteUser(id)
{
this.axios.delete('http://localhost:3000/posts/'+id).then((result)=>{
console.warn(result)
this.getUsers()
})
}
},
mounted()
{
this.getUsers()
},
}
</script>
- App.vue file:
<template>
<div id="app">
<h1>Delete Record with API</h1>
<User />
</div>
</template>
<script>
import User from "./components/User.vue";
export default {
name: "App",
components: {
User
}
};
</script>