vue3 axios post
时间: 2023-09-17 12:07:54 浏览: 113
Here's an example of how to make a POST request using Axios in Vue 3:
```
<template>
<button @click="submitForm">Submit Form</button>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
form: {
name: '',
email: ''
}
}
},
methods: {
async submitForm() {
try {
const response = await axios.post('/api/form', this.form);
console.log(response.data);
} catch (error) {
console.error(error);
}
}
}
}
</script>
```
In this example, we have a form with two fields: name and email. When the user submits the form, we make a POST request to the '/api/form' endpoint with the form data using `axios.post()`. The response data is then logged to the console.
Note that we're using the `async/await` syntax to handle the asynchronous nature of the `axios.post()` method. We're also wrapping the request in a `try/catch` block to handle any errors that might occur.
阅读全文