The update page is basically quite similar to the add page. This page will communicate with the server-side PUT route, /api/user/, to update the existing user with the following steps:
- Create a form to display the existing data and to collect the new data in the <template> block. The difference in the update page is the method that we bind to the <form> element:
// pages/users/update/_id.vue
<form v-on:submit.prevent="update">
//...
<button type="submit">Update</button>
</form>
- Create an update method to send the data to the server in the <script> block. We will use the asyncData method to fetch the existing data as follows:
// pages/users/update/_id.vue
export default {
async asyncData ({ params, error }) {
let { data } = await axios.get('/api/users/' + params.id)
let user = data.data
return {
id: user._id,
name: user.name,
slug...