如何使用 Axios (VueJS, Nuxt) 制作 .post

Mor*_*enz 4 vue.js axios nuxt.js

我是网络开发的新手。我想问一下如何.post用Axios用Nuxt创建一个。

我所需要的只是一个向 NodeJS 应用程序发送三个输入的按钮。

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-body">
                        <form @submit="formSubmit">
                        <strong>Name:</strong>
                        <input type="text" class="form-control" v-model="name">
                        <strong>Email:</strong>
                        <input type="text" class="form-control" v-model="email">
                        <strong>Password:</strong>
                        <input type="text" class="form-control" v-model="password">
                        <button class="btn btn-success">Submit</button>
                  </div>
            </div>
        </div>
    </div>
</template>

<script>
  export default {
    data() {
       return {
         name: '',
         email: '',
         password: ''
       };
    },

    methods: {
       //Would like to use the button to do this:
      async sendData () {
        await this.$axios.get('insert', {
          name: this.name, 
          email: this.email,
          password: this.password })
      }
     }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助。

Maj*_*awi 5

提交侦听器应改为调用该方法:

<form @submit="sendData">
Run Code Online (Sandbox Code Playgroud)

为了发送POST请求:

this.$axios.post('insert', {
          name: this.name, 
          email: this.email,
          password: this.password 
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});
Run Code Online (Sandbox Code Playgroud)

有关更多信息,您可以访问他们的页面