vue中的ElementUI的使用详解
登录+sessionStorage
效果展示
登录成功后会把用户id存入前端的sessionStorage,拦截器会根据是否存在用户id来进行拦截
也可以将用户权限存入sessionStorage,然后当访问某个页面的时候在created方法中判断是否具有权限
<template> <div class="login-wrap"> <el-form class="login-container" ref="loginFormRef" :model="loginForm"> <h1 class="title">用户登陆</h1> <el-form-item label=""> <el-input type="text" placeholder="用户账号" v-model="loginForm.username" autocomplete="off"></el-input> </el-form-item> <el-form-item label=""> <el-input type="text" placeholder="用户密码" v-model="loginForm.password" autocomplete="off"></el-input> </el-form-item> <el-form-item > <el-radio-group v-model="loginForm.resource"> <el-radio label="管理员"></el-radio> <el-radio label="普通用户"></el-radio> </el-radio-group> </el-form-item> <el-form-item> <template slot-scope="scope"> <el-button type="primary" @click="doLogin()" style="width: 100%;">用户登录</el-button> <!-- <el-button style="width: 48%;" @click="gotoRegister" >用户注册</el-button> --> </template> </el-form-item> <!-- <el-row style="text-align: center;"> <el-link @click="gotoRegister()">用户注册</el-link> <el-link>忘记密码</el-link> </el-row> --> </el-form> </div> </template> <script> export default { data: function() { return { loginForm: { username: '', password: '', resource: '' } } }, methods: { gotoRegister: function() { this.$router.push({ name: 'register' }); }, doLogin() { let resource = this.loginForm.resource this.$refs.loginFormRef.validate(async valid => { if (!valid) return; if (resource == "管理员") { const { data: res } = await this.axios.post("http://localhost:8088/loginadmin", this.loginForm) if (res) { this.$message.success("管理员登录成功") sessionStorage.setItem("personid","ljhjiayou") this.$router.push({ path: "/dong" }); } else { this.$message.error("账号或密码错误") } //普通用户登录 }else{ const { data: res } = await this.axios.post("http://localhost:8088/loginuser", this.loginForm) if (res) { console.log(res) this.$message.success("普通用户登录成功") sessionStorage.setItem("personid",res) this.$router.push({ path: "/person" }); } else { this.$message.error("账号或密码错误") } } }) } } } </script> //scoped="scoped" 表示样式仅仅规范当前VUE <style scoped="scoped"> .login-wrap { box-sizing: border-box; width: 100%; height: 100%; padding-top: 10%; background-image: url(data:image/svg+xml;base64,PD94b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MjMyLjc1IiByPSI1LjI1Ij48L2NpcmNsZT4KICAgICAgICAgICAgICAgIDwvZz4KICAgICAgICAgICAgPC9nPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+); /* background-color: #112346; */ background-repeat: no-repeat; background-position: center right; background-size: 100%; } .login-container { border-radius: 10px; margin: 0px auto; width: 350px; padding: 30px 35px 15px 35px; background: #fff; border: 1px solid #eaeaea; text-align: left; box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1); } .title { margin: 0px auto 40px auto; text-align: center; color: #505458; } </style>
分页表格
效果展示
分页这一块是难点也是重点!!!
handleSizeChange方法选择分页大小
handleCurrentChange方法选择第几页
loadData调用后端API接口获取分页的数据
<template> <div> <el-table :data="tableData" border style="width: 100%"> <el-table-column fixed prop="id" label="新闻id" width="150"> </el-table-column> <el-table-column prop="typeid" label="新闻标题" width="200"> </el-table-column> <el-table-column prop="otherid" label="新闻摘要" width="200"> </el-table-column> <el-table-column prop="money" label="新闻内容" width="200"> </el-table-column> <el-table-column prop="datetime" label="新闻发布时间" width="150"> </el-table-column> <el-table-column fixed="right" label="操作" width="200"> <template slot-scope="scope"> <el-button @click="updateExercise(scope.row)" size="mesdium" type="primary">修改</el-button> <el-button @click="deleteExercise(scope.row)" type="warning" size="medium">删除</el-button> </template> </el-table-column> </el-table> <div class="block"> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount"> </el-pagination> </div> </div> </template> <script> export default { methods: { handleSizeChange(val) { //console.log(val) this.pageSize = val; this.loadData( this.currentPage, this.pageSize); }, handleCurrentChange(val) { console.log(val) this.currentPage = val; this.loadData( this.currentPage, this.pageSize); }, //从服务器读取数据 loadData: function(pageNum, pageSize) { let _this = this; console.log(this.pageNum) this.axios.get("http://localhost:8088/records/querystudentbypage?pageNum=" + pageNum + "&pageSize=" + this.pageSize).then(function(res) { //console.log(res.data.pagestudentdata.list) _this.tableData = res.data.pagestudentdata.list; _this.totalCount = res.data.number; }, function() { console.log('failed'); }); }, }, data() { return { // currentPage1: 5, // currentPage2: 5, // currentPage3: 5, // currentPage4: 4, currentPage: 1, tableData: null, pageSize: 5, totalCount: 15, pageNum: 1, }; }, created() { // let _this = this; // axios.get("http://localhost:8088/records").then(function(response) { // _this.tableData = response.data // }) this.loadData(this.pageNum,this.pageSize) }, } </script>
后端
@ResponseBody @RequestMapping(value = "/querystudentbypage", method = RequestMethod.GET) public Map<String, Object> querystudentbypage(@RequestParam(value="pageNum")int pageNum, @RequestParam(value="pageSize")int pageSize) { PageHelper.startPage(pageNum,pageSize); List<Records> courses = recordsMapper.selectList(null); PageInfo<Records> page = new PageInfo<>(courses); System.out.println("---------------"+page); Map<String, Object> map = new HashMap<String, Object>(); map.put("pagestudentdata", page); map.put("number", page.getTotal()); return map; }
参考链接
开关+标签页
效果展示
开关的主要方法 switchChange
标签页不难,如果看不懂下面代码,可以参考官方文档
前端开关主要代码
<el-table-column prop="name" label="是否冻结" width="150"> <template slot-scope="scope"> <el-switch v-model="scope.row.statusid" :active-value="1" :inactive-value="2" @change="switchChange($event, scope.row)" active-color="#13ce66" inactive-color="#ff4949"></el-switch> </template> </el-table-column>
完整代码
<template> <el-tabs v-model="activeName" @tab-click="handleClick"> <el-tab-pane label="所有账户" name="first"> <el-table :data="tableData" border style="width: 100%"> <el-table-column fixed prop="accoundid" label="账户id" width="150"> </el-table-column> <el-table-column prop="password" label="密码" width="200"> </el-table-column> <el-table-column prop="balance" label="余额" width="200"> </el-table-column> <el-table-column prop="name" label="是否冻结" width="150"> <template slot-scope="scope"> <el-switch v-model="scope.row.statusid" :active-value="1" :inactive-value="2" @change="switchChange($event, scope.row)" active-color="#13ce66" inactive-color="#ff4949"></el-switch> </template> </el-table-column> <el-table-column prop="personid" label="personid" width="200"> </el-table-column> <el-table-column fixed="right" label="操作" width="200"> <template slot-scope="scope"> <el-button @click="deleteExercise(scope.row)" type="warning" size="medium" >删除</el-button > </template> </el-table-column> </el-table> </el-tab-pane> <el-tab-pane label="被冻结的账户" name="second"> <el-table :data="tableDatab" border style="width: 100%"> <el-table-column fixed prop="accoundid" label="账户id" width="150"> </el-table-column> <el-table-column prop="personinfo.realname" label="名字" width="200"> </el-table-column> <el-table-column prop="password" label="密码" width="200"> </el-table-column> <el-table-column prop="balance" label="余额" width="200"> </el-table-column> <el-table-column fixed="right" label="操作" width="200"> <template slot-scope="scope"> <el-button @click="qiyong(scope.row)" type="warning" size="medium" >启用</el-button > </template> </el-table-column> </el-table> </el-tab-pane> <el-tab-pane label="已启用的账户" name="third"> <el-table :data="tableDatabc" border style="width: 100%"> <el-table-column fixed prop="accoundid" label="账户id" width="150"> </el-table-column> <el-table-column prop="personinfo.realname" label="名字" width="200"> </el-table-column> <el-table-column prop="password" label="密码" width="200"> </el-table-column> <el-table-column prop="balance" label="余额" width="200"> </el-table-column> <el-table-column fixed="right" label="操作" width="200"> <template slot-scope="scope"> <el-button @click="dongjie(scope.row)" type="warning" size="medium" >冻结</el-button > </template> </el-table-column> </el-table> </el-tab-pane> </el-tabs> </template> <script> export default { data() { return { tableData: null, tableDatab: null, tableDatabc: null, activeName: 'first' }; }, methods: { handleClick(tab, event) { console.log(tab, event); }, switchChange(e, data) { if (e == 1) { axios.get('http://localhost:8088/accounts/kaiqi/' + data.accoundid).then((res) => { this.$message({ message: "开启成功", type: "success" }); // location.reload() }); } else if (e == 2) { axios.get('http://localhost:8088/accounts/dongjie/' + data.accoundid).then((res) => { this.$message({ message: "冻结成功", type: "success" }); // location.reload() }); } }, dongjie(row) { let _this=this this.$confirm('是否确定冻结'+row.personinfo.realname+'的账户?','冻结账户',{ confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(()=>{ axios.get('http://localhost:8088/accounts/dongjie/'+row.accoundid).then(function (response){ if(response.data){ _this.$alert(row.personinfo.realname+'的账户冻结成功!', '冻结账户',{ confirmButtonText: '确定', callback: action =>{ location.reload() } }); } }) }).catch(()=>{ }); }, qiyong(row) { let _this=this this.$confirm('是否确定启用'+row.personinfo.realname+'的账户?','启用账户',{ confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(()=>{ axios.get('http://localhost:8088/accounts/kaiqi/'+row.accoundid).then(function (response){ if(response.data){ _this.$alert(row.personinfo.realname+'的账户启用成功!', '启用账户',{ confirmButtonText: '确定', callback: action =>{ location.reload() } }); } }) }).catch(()=>{ }); }, deleteExercise(row) { let _this=this this.$confirm('是否确定删除账户'+row.accoundid+'?','删除账户',{ confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(()=>{ axios.delete('http://localhost:8088/accounts/'+row.accoundid).then(function (response){ if(response.data){ _this.$alert(row.accoundid+'账户删除成功!', '删除账户',{ confirmButtonText: '确定', callback: action =>{ location.reload() } }); } }) }).catch(()=>{ }); }, }, created(){ let _this=this; axios.get("http://localhost:8088/accounts").then(function (response){ _this.tableData=response.data }) axios.get("http://localhost:8088/accounts/dongjie").then(function (response){ _this.tableDatab=response.data }) axios.get("http://localhost:8088/accounts/qiyong").then(function (response){ _this.tableDatabc=response.data }) }, }; </script>
前端拦截器
判断路由是否需要登录权限
main.js
router.beforeEach((to, from, next) => { // next() if (to.meta.requireAuth) { // 判断该路由是否需要登录权限 if (sessionStorage.getItem("personid")) { next() } else { // 未登录,跳转到登录页面 next('/') } } else { next() } })
route.js
在路由中添加 meta: requireAuth:true
表示是否需要登录才能访问
{ path: '/listxw', name: '新闻管理', component: Index, show: true, meta: { requireAuth: true }, redirect: "/listxw", children: [{ path: "/addxw", name: "添加新闻", component: addxw, meta: { requireAuth: true }, }, { path: "/updatexw", name: "修改新闻", component: updatexw, meta: { requireAuth: true }, }, { path: '/listxw', name: '新闻列表', component: listxw, meta: { requireAuth: true }, }, { path: '/person', name: '个人信息', component: person, meta: { requireAuth: true }, }, { path: '/record', name: '交易记录', component: record, meta: { requireAuth: true }, } ], },
富文本编辑器
效果展示
下面几个名字是使用了富文本编辑器的回显
首先需要安装Vue-Quill-Editor
命令:npm install vue-quill-editor --save
下载quill(Vue-Quill-Editor需要依赖)
npm install quill --save
完整代码
<template> <div> <el-form ref="loginFormRef" label-width="100px" style="width: 60%" :model="loginForm"> <el-form-item label="新闻标题"> <el-input v-model="loginForm.title"></el-input> </el-form-item> <el-form-item label="新闻摘要"> <el-input v-model="loginForm.newsabstract"></el-input> </el-form-item> <el-form-item label="新闻内容"> <div class="edit_container" style="width: 100%"> <quill-editor v-model="loginForm.text" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"> </quill-editor> </div> </el-form-item> <el-form-item> <el-button type="primary" @click="changePwd()">添加新闻</el-button> </el-form-item> </el-form> <div v-html="str" class="ql-editor"> {{str}} </div> </div> </template> <script> import { quillEditor } from "vue-quill-editor"; //调用编辑器 import 'quill/dist/quill.core.css'; import 'quill/dist/quill.snow.css'; import 'quill/dist/quill.bubble.css'; export default { components: { quillEditor }, data() { return { loginForm: { title: "", newsabstract: "", text: "", releasedate: "", }, str: '', }; }, methods: { changePwd: function() { this.$refs.loginFormRef.validate(async valid => { if (!valid) return; const { data: res } = await this.axios.put("http://localhost:8088/news", this.loginForm) if (res) { this.$message.success("添加新闻成功") this.$router.push({ path: "/listxw" }); } else { this.$message.error("添加新闻失败") } }) }, onEditorReady(editor) { // 准备编辑器 }, onEditorBlur() {}, // 失去焦点事件 onEditorFocus() {}, // 获得焦点事件 onEditorChange() {}, // 内容改变事件 // 转码 escapeStringHTML(str) { str = str.replace(/</g, '<'); str = str.replace(/>/g, '>'); return str; } }, computed: { editor() { return this.$refs.myQuillEditor.quill; }, }, mounted() { let content = '<ul><li><strong>李佳豪</strong></li><li><strong><u>陈刚</u></strong></li><li><strong><s><u>李金铭</u></s></strong></li><li><em>杨军</em></li></ul>'; // 请求后台返回的内容字符串 this.str = this.escapeStringHTML(content); } }; </script> <style> </style>
vue中富文本编辑器使用参考链接
本地源代码
前端E:\VNC\project\capital_vue
后端E:\VNC\project\capital
到此这篇关于vue中的ElementUI的使用详解的文章就介绍到这了,更多相关vue ElementUI 使用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)