类型错误:无法读取未定义的属性(读取"输入"),Vue



我试图访问一个使用我的API,当我尝试使用邮差时,从VUE,但由于某种原因,我得到错误"TypeError:无法读取未定义的属性(读取'输入')"

发送邮件时的正文应该是这样的:

文章:http://localhost: 8080/api/vendedor/登录

{
"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: Cannot read properties of undefined (reading 'input')">

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

*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);
}
}
}*

最新更新