使用组件库的消息提示组件 | Element
按需导入
import Vue from 'vue'
/* 需要按需导入 */
import { Button, Form, FormItem, Input, Message } from 'element-ui'
/* 需要注册成为全局可用 的中间件 */
Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.prototype.$message = Message // 需要全局挂载
login(){
this.$refs.loginFormRef.validate(async(valid)=>{//(valid)种的()可以省略 因为只有一个形参
if(!valid) return;//false发起请求失败
//url 数据(前面双向绑定的数据)
const {data:res}= await this.$http.post('login',this.loginForm)
if(res.meta.status!==200) return this.$message.error('登录失败')
this.$message.success('登录成功')//
})
}
<template>
<div class="login_container">
<!-- 登录盒子布局 -->
<div class="login_box">
<!-- 头像布局 -->
<div class="avatar_box">
<img src="../assets/a1.png" alt="">
</div>
<!-- 表单布局 -->
<!-- label-width文本框顶端对齐 -->
<el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
<!-- 用户名 -->
<el-form-item prop="username">
<el-input put v-model="loginForm.username" prefix-icon="iconfont
icon-yonghus"></el-input>
</el-form-item>
<!-- 密码 -->
<el-form-item prop="password">
<el-input v-model="loginForm.password" prefix-icon="iconfont
icon-mima" type="password"></el-input>
</el-form-item>
<!-- 按钮区域 -->
<el-form-item class="btns">
<el-button type="success" @click="login" round>登录</el-button>
<el-button type="danger" @click="resetLoginForm" round>重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {//这是登录表单的数据绑定对象
//数据属性
username: '',
password: ''
},
loginFormRules: {//这是登录表单的数据验证规则对象
username: [
{ required: true, message: '请输入用户名称', trigger: 'blur' },
{ min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
],//验证用户名是否合法
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
]//验证密码是否合法
}
}
},
methods:{
//点击重置按钮,重装
resetLoginForm(){
this.$refs.loginFormRef.resetFields()
},
login(){
this.$refs.loginFormRef.validate(async(valid)=>{//(valid)种的()可以省略 因为只有一个形参
if(!valid) return;//false发起请求失败
//url 数据(前面双向绑定的数据)
const {data:res}= await this.$http.post('login',this.loginForm)
if(res.meta.status!==200) return this.$message.error('登录失败')
this.$message.success('登录成功')//
})
}
},
}
</script>
<style lang="less" scoped>
.login_container {
background-color: #2b4b6b;
height: 100%;
}
.login_box {
width: 450px;
height: 300px;
background-color: #fff;
border-radius: 3px;
/*圆角边框 */
/* 居中 */
position: absolute;
/*则向相当于div进行定位*/
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.avatar_box {
height: 130px;
width: 130px;
border: 4px solid orange;
border-radius: 50%;
padding: 5px;
box-shadow: 0 0 10px #eee;
/* 移动位置 居中*/
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
img {
width: 100%;
height: 100%;
/* 图片圆角边框 */
border-radius: 50%;
box-shadow: 0 0 5px #2b4b6b;
/* background-color: yellow; 如果图片很小 +背景色 */
}
}
/* 表单在页面底部 */
.login_form {
position: absolute;
width: 100%;
bottom: 0;
padding: 0 20px;
/* 不然会溢出 */
box-sizing: border-box;
}
/* 按钮在页面底部 */
.btns {
display: flex;
justify-content: flex-end;
}
</style>