Vue element 写个人中心html
时间: 2024-04-27 18:20:06 浏览: 152
以下是一个使用 Vue.js 和 Element UI 组件库编写的个人中心页面的示例:
```html
<template>
<div class="personal-center">
<el-container>
<el-header>
<h1 class="site-title">个人中心</h1>
<el-menu class="site-nav" mode="horizontal" :default-active="activeTab" @select="handleTabClick">
<el-menu-item index="home">首页</el-menu-item>
<el-submenu index="profile">
<template slot="title">个人信息</template>
<el-menu-item index="basic">基本信息</el-menu-item>
<el-menu-item index="security">安全设置</el-menu-item>
</el-submenu>
<el-menu-item index="orders">我的订单</el-menu-item>
<el-menu-item index="logout">退出</el-menu-item>
</el-menu>
</el-header>
<el-main>
<el-row>
<el-col :span="18">
<el-tabs v-model="activeTab" type="border-card">
<el-tab-pane label="基本信息" name="basic">
<el-form label-position="left" label-width="100px" :model="userInfo" :rules="rules" ref="userInfoForm">
<el-form-item label="用户名" prop="username">
<el-input v-model="userInfo.username" :disabled="true"></el-input>
</el-form-item>
<el-form-item label="昵称" prop="nickname">
<el-input v-model="userInfo.nickname"></el-input>
</el-form-item>
<el-form-item label="电子邮件" prop="email">
<el-input v-model="userInfo.email"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="updateUserInfo">更新信息</el-button>
</el-form-item>
</el-form>
</el-tab-pane>
<el-tab-pane label="安全设置" name="security">
<el-form label-position="left" label-width="100px" :model="securityInfo" :rules="securityRules" ref="securityInfoForm">
<el-form-item label="原密码" prop="oldPassword">
<el-input type="password" v-model="securityInfo.oldPassword"></el-input>
</el-form-item>
<el-form-item label="新密码" prop="newPassword">
<el-input type="password" v-model="securityInfo.newPassword"></el-input>
</el-form-item>
<el-form-item label="确认新密码" prop="confirmPassword">
<el-input type="password" v-model="securityInfo.confirmPassword"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="updateSecurityInfo">更新密码</el-button>
</el-form-item>
</el-form>
</el-tab-pane>
<el-tab-pane label="我的订单" name="orders">
<el-table :data="orders" style="width: 100%">
<el-table-column prop="orderId" label="订单号"></el-table-column>
<el-table-column prop="productName" label="产品名"></el-table-column>
<el-table-column prop="price" label="价格"></el-table-column>
<el-table-column prop="status" label="状态"></el-table-column>
</el-table>
</el-tab-pane>
</el-tabs>
</el-col>
<el-col :span="6">
<el-card shadow="hover">
<div slot="header">
<h3>个人资料</h3>
</div>
<div class="user-info">
<div class="avatar">
<el-avatar size="large" :src="userInfo.avatar"></el-avatar>
</div>
<div class="username">{{ userInfo.username }}</div>
<div class="nickname">{{ userInfo.nickname }}</div>
</div>
</el-card>
</el-col>
</el-row>
</el-main>
<el-footer class="site-footer">
<p>© 2021 My Website. All rights reserved.</p>
</el-footer>
</el-container>
</div>
</template>
<script>
import { mapState } from 'vuex';
export default {
computed: {
...mapState(['userInfo', 'orders']),
},
data() {
return {
activeTab: 'basic',
securityInfo: {
oldPassword: '',
newPassword: '',
confirmPassword: '',
},
rules: {
nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
email: [
{ required: true, message: '请输入电子邮件', trigger: 'blur' },
{ type: 'email', message: '请输入正确的电子邮件格式', trigger: ['blur', 'change'] },
],
},
securityRules: {
oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
newPassword: [
{ required: true, message: '请输入新密码', trigger: 'blur' },
{ min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
],
confirmPassword: [
{ required: true, message: '请再次输入新密码', trigger: 'blur' },
{ validator: this.validateConfirmPassword, trigger: 'blur' },
],
},
};
},
methods: {
handleTabClick(tab) {
this.$router.push({ path: '/personal-center/' + tab.index });
},
updateUserInfo() {
this.$refs.userInfoForm.validate((valid) => {
if (valid) {
// 提交表单数据到后端
this.$message.success('更新成功');
} else {
this.$message.error('表单验证失败');
}
});
},
updateSecurityInfo() {
this.$refs.securityInfoForm.validate((valid) => {
if (valid) {
// 提交表单数据到后端
this.$message.success('密码更新成功');
} else {
this.$message.error('表单验证失败');
}
});
},
validateConfirmPassword(rule, value, callback) {
if (value !== this.securityInfo.newPassword) {
callback(new Error('两次输入的密码不一致'));
} else {
callback();
}
},
},
};
</script>
<style>
.personal-center {
height: 100%;
}
.site-title {
color: #fff;
margin: 0;
}
.site-nav {
float: right;
margin-top: 20px;
}
.site-footer {
text-align: center;
color: #ccc;
padding: 20px;
}
.user-info {
text-align: center;
margin-top: 20px;
}
.user-info .avatar {
margin-bottom: 10px;
}
.user-info .username {
font-size: 16px;
font-weight: bold;
margin-bottom: 5px;
}
.user-info .nickname {
font-size: 14px;
}
.el-tabs__header {
border-bottom: none;
}
.el-tabs__nav-wrap {
margin-bottom: 10px;
}
.el-tab-pane {
padding: 20px;
}
.el-form-item__label {
font-weight: bold;
}
.el-button {
margin-top: 20px;
}
.el-table {
margin-top: 20px;
}
.el-card__header {
padding: 10px;
}
.el-card__body {
padding: 20px;
text-align: center;
}
</style>
```
这个页面使用了 Element UI 组件库中的很多组件,包括头部导航菜单、选项卡、表单、表格、卡片等。用户可以在个人信息选项卡中更新个人资料,安全设置选项卡中更新密码,订单选项卡中查看订单信息。通过使用 Vue.js 的路由功能,当用户点击导航菜单时,页面会自动跳转到对应的选项卡。
阅读全文