Created
April 19, 2019 02:09
-
-
Save kabitacode/7fbcfd8d459555bca31e43405af2ed47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<form @submit.prevent="add"> | |
<input type="hidden" v-model="form.id"> | |
<input type="text" v-model="form.name"> | |
<button type="submit" v-show="!updateSubmit">add</button> | |
<button type="button" v-show="updateSubmit" @click="update(form)">Update</button> | |
</form> | |
<ul v-for="user in users" :key="user.idd"> | |
<li> | |
<span>{{user.name}}</span>   | |
<button @click="edit(user)">Edit</button> || <button @click="del(user)">Delete</button> | |
</li> | |
</ul> | |
</div> | |
</template> | |
<script> | |
/* eslint-disable */ | |
import axios from 'axios' | |
export default { | |
data(){ | |
return{ | |
form: { | |
id: '', | |
name: '' | |
}, | |
users: '', | |
updateSubmit: false | |
} | |
}, | |
mounted() { | |
this.load() | |
}, | |
methods: { | |
load(){ | |
axios.get('http://localhost:3000/users').then(res => { | |
this.users = res.data | |
}).catch ((err) => { | |
console.log(err); | |
}) | |
}, | |
add(){ | |
axios.post('http://localhost:3000/users/', this.form).then(res => { | |
this.load() | |
this.form.name = '' | |
}) | |
}, | |
edit(user){ | |
this.updateSubmit = true | |
this.form.id = user.id | |
this.form.name = user.name | |
}, | |
update(form){ | |
return axios.put('http://localhost:3000/users/' + form.id , {name: this.form.name}).then(res => { | |
this.load() | |
this.form.id = '' | |
this.form.name = '' | |
this.updateSubmit = false | |
}).catch((err) => { | |
console.log(err); | |
}) | |
}, | |
del(user){ | |
axios.delete('http://localhost:3000/users/' + user.id).then(res =>{ | |
this.load() | |
let index = this.users.indexOf(form.name) | |
this.users.splice(index,1) | |
}) | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
incredible