axios TypeError:无法读取undefined的属性(阅读'input'),Vue

bbmckpt7  于 2023-02-03  发布在  iOS
关注(0)|答案(1)|浏览(174)

我尝试使用VUE中的API访问,当我尝试使用Postman时,该API正常工作,但由于某种原因,我收到错误“TypeError:无法读取undefined(阅读'input')"的属性。
正文在发送帖子时会是这样的:
开机自检:http://localhost:8080/api/供应商/登录

{
  "correo": "l.andrade01@ufromail.cl",
  "password": "123456"
}

来自POST的答案将是一个JSON,其中包含:

{
  "idvendedor": 5,
  "nombre": "Leonardo",
  "apellido": "Andrade",
  "correo": "l.andrade01@ufromail.cl",
  "password": "123456",
  "lugartrabajo": "Casino Los Notros"
}

登录的HTML如下所示:

<form class="w-96 mx-auto rounded-md">
            <div class="input">
                <label for="email" class="text-xl font-medium text- flex justify-left py-1">Correo</label>
                <input type="text" name="email" v-model="input.email" placeholder="emailejemplo@ufromail.cl" class="border-2 p-1 w-96 border-violet-700 rounded-full">
            </div>
            <div class="input">
                <label for="password" class="text-xl font-medium text- flex justify-left py-1">Contraseña</label>
                <input type="password" name="password" v-model="input.password" placeholder="***************************" class="border-2 p-1 w-96 border-violet-700 rounded-full">
            </div>
            <div class="pb-5"></div>
            <button type="submit" id="botonlogin" v-on:click.prevent="login()" class="ml-28 h-8 w-36 mx-auto bg-gradient-to-r from-indigo-500 to-indigo-700 rounded-full hover:from-indigo-400 hover:to-indigo-600">
                <span class="text-center text-white font-medium">Iniciar Sesión</span>
            </button>
        </form>

下面是登录中的脚本:

<script>
import axios from 'axios';

  export default {
    name: 'Login',
    data() {
      return {
        input: {
          email: "",
          password: ""
        },
        vendedor: {
          idvendedor: "",
          nombre: "",
          apellido: "",
          correo: "",
          password: "",
          lugartrabajo: ""
        },
      }
    },
    methods: {
      async login() {
        try{
          let res = await axios.post('http://localhost:8080/api/vendedor/login', this.data.input);
          console.log(res);
          if(this.input.email == this.vendedor.correo && this.input.password == this.vendedor.password){
            this.$router.push('/vendedor/homeVender');
          }
        }catch (e){
          console.log(e)
        }
        
          
      }
    }
  }
</script>

我希望从axios获取JSON,以便可以为登录设置“if,”但我收到了错误“TypeError:无法读取未定义的属性(阅读'input')”

ep6jt1vc

ep6jt1vc1#

您看到的错误是因为您试图访问登录方法中数据对象的input属性,但Vue组件中没有data属性。要修复此错误,只需在登录方法中将此.data.input替换为此.input即可:

*methods: {
  async login() {
    try {
      let res = await axios.post('http://localhost:8080/api/vendedor/login', this.input);
      console.log(res);
      if (this.input.email == this.vendedor.correo && this.input.password == this.vendedor.password) {
        this.$router.push('/vendedor/homeVender');
      }
    } catch (e) {
      console.log(e);
    }
  }
}*

相关问题