饿了么UI+VUE+axios向尚硅谷node后端验证登录。
遇到问题:传递参数时,如果只传递一个参数可以验证,但是传递账号+密码时提示让我输入密码。
解决:用字符串拼接,只传一个参数,第二个参数用&连接起来。
<template lang="">
<div class="detail" style="display: flex;">
<el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px"
style="width: 32%;height: 50%;margin: auto;">
<el-form-item label="登录账号:" prop="account" v-if="showArea">
<el-input v-model="ruleForm.account" placeholder="请输入账号" style="width: 20rem;"></el-input>
</el-form-item>
<el-form-item label="注册账号:" prop="account1" v-if="showArea1">
<el-input v-model="ruleForm.account1" placeholder="请输入注册账号" style="width: 20rem;"></el-input>
</el-form-item>
<el-form-item label="登录密码:" prop="pass" v-if="showArea">
<el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="请输入密码"
style="width: 20rem;"></el-input>
</el-form-item>
<el-form-item label="注册密码:" prop="pass1" v-if="showArea1">
<el-input type="password" v-model="ruleForm.pass1" autocomplete="off" placeholder="请输入注册密码"
style="width: 20rem;"></el-input>
</el-form-item>
<el-form-item>
<!-- <el-button type="primary" @click="submitForm('ruleForm')" style="width: 20rem;" v-if="showArea">登录
</el-button> -->
<el-button type="primary" @click="denglu()" style="width: 20rem;" v-if="showArea">登录
</el-button>
<el-button type="primary" @click="submitForm('ruleForm1')" style="width: 20rem;" v-if="showArea1">注册
</el-button>
<el-link :underline="false" style="float: right;" v-on:click="topNone">{{qiehuan}}</el-link>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
ruleForm: {
pass: '',
account: '',
pass1: '',
account1: '',
},
showArea: true,//切换注册
showArea1: false,//切换登录
qiehuan:'去注册账号'
};
},
methods: {
// submitForm(formName) {
// const axios = require('axios');
// var params = new URLSearchParams();
// this.$refs[formName].validate((valid) => {
// if (valid) {
// params.append('username', 'admin2');
// params.append('password', '000000');
// axios.post('http://127.0.0.1:3007/api/login', params)
// .then(function (value) {
// console.log(value);
// })
// .catch(function (reason) {
// console.log(reason);
// });
// } else {
// console.log('error submit!!');
// return false;
// }
// });
// },
topNone() {
if (this.showArea) {
this.showArea = false;
this.showArea1 = true;
this.qiehuan="去登录"
} else {
this.showArea = true;
this.showArea1 = false;
this.qiehuan="去注册账号"
}
},
denglu() {
const that=this;
const axios = require('axios');
axios.post('http://127.0.0.1:3007/api/login', 'username='+that.ruleForm.account + '&password='+that.ruleForm.pass)
.then(function (value) {
console.log(value.data);
})
.catch(function (reason) {
console.log(reason);
});
}
},
}
</script>