router下index.js文件配置路由:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'regis',
component:()=>import('@/views/RegisView.vue')
},
]
const router = new VueRouter({
routes
})
export default router
App.vue引用:
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<style lang="scss">
</style>
在views文件下编写RegisView.vue文件:
<template>
<div class="myform">
<el-form
:model="ruleForm"
status-icon
:rules="rules"
ref="ruleForm"
label-width="100px"
>
<el-form-item label="用户名" prop="username">
<el-input v-model="ruleForm.username"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="ruleForm.email"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input
type="password"
v-model="ruleForm.password"
autocomplete="off"
></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="checkPass">
<el-input
type="password"
v-model="ruleForm.checkPass"
autocomplete="off"
></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')"
>注册</el-button
>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
name: "LoginView",
data() {
var checkUser = (rule, value, callback) => {
console.log("用户名:", value);
if (value.trim() == "") {
callback(new Error("请输入用户名"));
} else if (!/^[0-9a-zA-Z_\u4e00-\u9fa5]{5,10}$/.test(value)) {
callback(new Error("用户名为5-10位中英文数字或者下划线"));
} else {
callback();
}
};
var checkEmail = (rule, value, callback) => {
console.log("邮箱:", value);
if (value.trim() == "") {
callback(new Error("邮箱不能为空"));
} else if (value.indexOf("@") == -1 || value.indexOf(".") == -1) {
callback(new Error("邮箱需要含有@和."));
} else if (value.length < 6) {
callback(new Error("邮箱需大于6位"));
} else {
callback();
}
};
var validatePass = (rule, value, callback) => {
console.log("密码:", value);
if (value.trim() == "") {
callback(new Error("请输入密码"));
} else {
callback();
}
};
var validatePass2 = (rule, value, callback) => {
if (value === "") {
callback(new Error("请再次输入密码"));
} else if (value !== this.ruleForm.password) {
callback(new Error("两次输入密码不一致!"));
} else {
callback();
}
};
return {
ruleForm: {
password: "",
username: "",
checkPass: "",
email: "",
},
rules: {
password: [{ validator: validatePass, trigger: "blur" }],
email: [{ validator: checkEmail, trigger: "blur" }],
username: [{ validator: checkUser, trigger: "blur" }],
checkPass: [{ validator: validatePass2, trigger: "blur" }],
},
};
},
from表单的登录页面
el-form 组件 :model="ruleForm" ruleForm是data中定义的存储的是用户名和密码值的对象 通过model传给el-form组件
:rules="rules" rules是data中定义的对象,目的是校验用户名和密码的规则
-- ref="ruleForm" 咱们可以通过ref来获取el-form组件内部的方法 比如:validate校验方法 resetFields重置方法
status-icon 是在表单校验错误的时候 输入框中出现的提示小图标
--label-width="200px" 是用来控制用户名和密码文本的宽度
<template>
<div class="myform">
<h1>用户登录</h1>
<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" >
<!-- label 控制输入框的文本 prop="username" 是对应表单域model中的username字段 规则的名字需要和表单域model中的字段一模一样 -->
<el-form-item label="用户名" prop="username">
<el-input v-model="ruleForm.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<!-- autocomplete="off"作用是把输入框的自动提示功能关闭 -->
<el-input type="password" v-model="ruleForm.password" autocomplete="off" ></el-input>
</el-form-item>
<el-form-item> <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form> </div></template>
<script>
export default {
name: "LoginView",
data() {
var checkUser = (rule, value, callback) => {
console.log("用户名:", value);
if (value.trim() == "") {
callback(new Error("请输入用户名"));
} else if (!/^[\w\u4e00-\u9fa5]{5,10}$/.test(value)) {
callback(new Error("用户名为5-10位中英文数字或者下划线"));
} else {
callback();
}
};
var validatePass = (rule, value, callback) => {
console.log("密码:", value);
if (value.trim() == "") {
callback(new Error("请输入密码"));
} else {
callback();