vue使用element写东西让我感觉到了特别的方便,但是一遇到问题的话,那是真还好,网上基本上都有一些小demo或详解,今天我这里给大家分享下我写的一个简单的登陆跳转的页面
html代码
账号,密码和确认密码
<template>
<div class="Land">
<div class="topzi">图书管理后台登陆</div>
<el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
<el-form-item prop="name" label="姓名">
<el-input v-model="ruleForm2.name"></el-input>
</el-form-item>
<el-form-item label="密码" prop="pass">
<el-input type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="checkPass">
<el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
<el-button @click="resetForm('ruleForm2')">重置</el-button>
</el-form-item>
</el-form>
</div>
</template>
css代码
由于用的element组件写的标签基于app下
<style>
html,body{
width:100%;
height:100%;
background: url(../../build/beijin.jpg) no-repeat;
background-size: 100% 100%;
}
#app .topzi{
font-size: 40px;
margin-bottom: 20px;
}
#app .Land{
width: 30%;
position:fixed;
top:20%;
left: 0;
right: 0;
margin:0 auto;
border: 1px solid #000;
padding: 40px;
}
</style>
js事件
data下的判断,return下的规范,methods下的事件
<script>
export default {
data() {
var checkAge = (rule, value, callback) => {
if (!value) {
return callback(new Error('账号不能为空'));
}
};
var validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入密码'));
} else {
if (this.ruleForm2.checkPass !== '') {
this.$refs.ruleForm2.validateField('checkPass');
}
callback();
}
};
var validatePass2 = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.ruleForm2.pass) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
return {
ruleForm2: {
pass: '',
checkPass: '',
name: ''
},
rules2: {
pass: [
{ required: true,validator: validatePass, trigger: 'blur' }
],
checkPass: [
{ required: true,validator: validatePass2, trigger: 'blur' }
],
name: [{required: true, message: '不能为空', trigger: 'blur'},
{pattern: /^[\u4E00-\u9FA5]+$/, message: '用户名只能为中文', trigger: 'blur'}],
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
this.$router.push({path:'/HelloWorld'})
} else {
console.log('error submit!!');
// return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
希望我写的整个能够帮助到你们
来源:oschina
链接:https://my.oschina.net/u/4391460/blog/3317064