1.ElementUI之CUD
- //src/api/action.js
- /**
- * 对后台请求的地址的封装,URL格式如下:
- * 模块名_实体名_操作
- */
- export default {
- 'SERVER': 'http://localhost:8080/ssm', //服务器
- 'SYSTEM_USER_DOLOGIN': '/user/userLogin', //登陆
- 'SYSTEM_USER_DOREG': '/user/userRegister', //注册
- 'SYSTEM_MENUS': '/module/queryRootNode', //左侧菜单树
- 'BOOK_LIST': '/book/queryBookPager', //书籍列表
- 'BOOK_ADD': '/book/addBook', //书籍增加
- 'BOOK_UPD': '/book/editBook', //书籍修改
- 'BOOK_DEL': '/book/delBook', //书籍删除
- 'getFullPath': k => { //获得请求的完整地址,用于mockjs测试时使用
- return this.SERVER + this[k];
- }
- }
- //src/main.js
- // The Vue build version to load with the `import` command
- // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
- import Vue from 'vue'
- //开发环境:true && require('@/mock')
- //生产环境:false && require('@/mock')
- //开发环境下才会引入mockjs
- // process.env.MOCK && require('@/mock')
- // 新添加1
- import ElementUI from 'element-ui'
- // 新添加2,避免后期打包样式不同,要放在import App from './App';之前
- import 'element-ui/lib/theme-chalk/index.css'
-
- import App from './App'
- import router from './router'
-
- import axios from '@/api/http'
- import VueAxios from 'vue-axios'
-
- Vue.use(VueAxios,axios)
-
- Vue.use(ElementUI);
- Vue.config.productionTip = false
- /* eslint-disable no-new */
-
-
- new Vue({
- el: '#app',
- router,
- data(){
- return{
- Bus:new Vue()
- }
- },
- router,
- components: { App },
- template: '
' - })
- //src/views/book/BookList.vue
- "books" style="padding: 20px;">
-
-
"true" class="demo-form-inline"> -
"书籍名称"> -
"bookname" placeholder="书籍名称"> -
-
-
"primary" @click="onSubmit">查询 -
"primary" @click="open">新增 -
-
-
-
"tableData" stripe style="width: 100%"> -
"id" label="书籍编号" width="180"> -
-
"bookname" label="书籍名称" width="180"> -
-
"price" label="书籍价格"> -
-
"type" label="书籍类别"> -
-
"操作"> - "scope">
-
"mini" @click="open(scope.$index, scope.row)">编辑 -
"mini" type="danger" @click="del(scope.$index, scope.row)">删除 -
-
-
-
- "block">
- "demonstration">完整功能
-
@size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" - :page-sizes="[10, 20, 30, 40]" :page-size="rows" layout="total,sizes, prev, pager, next, jumper" :total="50">
-
-
-
-
"title" :visible.sync="dialogFormVisible" @close="clear"> -
"book"> -
"书籍编号" :label-width="formLabelWidth"> -
"book.id" autocomplete="off"> -
-
"书籍名称" :label-width="formLabelWidth"> -
"book.bookname" autocomplete="off"> -
-
"书籍价格" :label-width="formLabelWidth"> -
"book.price" autocomplete="off"> -
-
"书籍类别" :label-width="formLabelWidth"> -
"book.booktype" placeholder="请选择活动区域"> -
for="t in types" :label="t.name" :value="t.name" :key="'key_'+t.id"> -
-
-
- "footer" class="dialog-footer">
-
@click="dialogFormVisible = false">取 消 -
"primary" @click="dosub ">确 定 -
-
-
-
- export default {
- data() {
- return {
- bookname: "",
- tableData: [],
- rows: 10,
- page: 1,
- total: 0,
- title: '新增窗体',
- dialogFormVisible: false,
- formLabelWidth: '100px',
- types: [],
- book: {
- id: '',
- bookname: '',
- price: '',
- booktype: ''
- }
- }
- },
- methods: {
- del(idx, row) {
- this.$confirm('此操作将永久删除id为' + row.id + '的数据, 是否继续?', '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning'
- }).then(() => {
- let url = this.axios.urls.BOOK_DEL;
- this.axios.post(url, {id:row.id}).then(r => {
- console.log(r);
- this.$message({
- type: 'seccess',
- message: '删除成功'
- });
- this.query({});
-
- }).catch(() => {
- });
- });
- },
- dosub() {
- let url = this.axios.urls.BOOK_ADD;
- if (this.title == '编辑窗体') {
- url = this.axios.urls.BOOK_UPD;
- }
- let params = {
- id: this.book.id,
- bookname: this.book.bookname,
- price: this.book.price,
- booktype: this.book.booktype
- };
- this.axios.post(url, params).then(r => {
- console.log(r);
- this.clear();
- this.query({});
- }).catch(e => {
-
- })
- },
- clear() {
- // 初始窗体化
- this.dialogFormVisible = false;
- this.title = '新增窗体';
- this.book = {
- id: '',
- bookname: '',
- price: '',
- booktype: ''
- }
- },
- open(idx, row) {
- // 打开窗体的方法
- this.dialogFormVisible = true;
- if (row) {
- this.title = '编辑窗体';
- this.book.id = row.id;
- this.book.bookname = row.bookname;
- this.book.price = row.price;
- this.book.booktype = row.booktype;
- }
- },
- query(params) {
- let url = this.axios.urls.BOOK_LIST;
- this.axios.get(url, {
- params: params
- }).then(r => {
- console.log(r);
- this.tableData = r.data.rows;
- this.total = r.data.total;
- }).catch(e => {})
- },
- onSubmit() {
- let params = {
- bookname: this.bookname
- }
- this.query(params);
- },
- handleSizeChange(r) {
- //当页大小变化
- console.log("当前页大小为:" + r);
- let params = {
- bookname: this.bookname,
- rows: r,
- page: this.page
- }
- this.query(params);
- },
- handleCurrentChange(p) {
- //当前页码变化
- console.log("当前页码为:" + p);
- let params = {
- bookname: this.bookname,
- rows: p,
- page: this.page
- }
- this.query(params);
- },
- },
- created() {
- this.query({});
- this.types = [{
- id: 1,
- name: '爽文'
- },
- {
- id: 2,
- name: '动作'
- },
- {
- id: 3,
- name: '爱情'
- },
- {
- id: 4,
- name: '国外'
- },
- {
- id: 5,
- name: '伦理'
- }
- ];
- }
- }
-
2.表单验证
- //src/router/index.js
- import Vue from 'vue'
- import Router from 'vue-router'
- import HelloWorld from '@/components/HelloWorld'
- import AppMain from '@/components/AppMain'
- import LeftNav from '@/components/LeftNav'
- import TopNav from '@/components/TopNav'
- import Login from '@/views/Login'
- import Register from '@/views/Register'
- import AddBook from '@/views/book/AddBook'
- import BookList from '@/views/book/BookList'
- Vue.use(Router)
-
- export default new Router({
- routes: [{
- path: '/',
- name: 'Login',
- component: Login
- },
- {
- path: '/Register',
- name: 'Register',
- component: Register
- },
- {
- path: '/AppMain',
- name: 'AppMain',
- component: AppMain,
- children: [{
- path: '/LeftNav',
- name: 'LeftNav',
- component: LeftNav
- },
- {
- path: '/TopNav',
- name: 'TopNav',
- component: TopNav
- },
- {
- path: '/book/AddBook',
- name: 'AddBook',
- component: AddBook
- },
- {
- path: '/book/BookList',
- name: 'BookList',
- component: BookList
- }]
- }
- ]
-
- })
- //src/main.js
- // The Vue build version to load with the `import` command
- // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
- import Vue from 'vue'
- //开发环境:true && require('@/mock')
- //生产环境:false && require('@/mock')
- //开发环境下才会引入mockjs
- // process.env.MOCK && require('@/mock')
- // 新添加1
- import ElementUI from 'element-ui'
- // 新添加2,避免后期打包样式不同,要放在import App from './App';之前
- import 'element-ui/lib/theme-chalk/index.css'
-
- import App from './App'
- import router from './router'
-
- import axios from '@/api/http'
- import VueAxios from 'vue-axios'
-
- Vue.use(VueAxios,axios)
-
- Vue.use(ElementUI);
- Vue.config.productionTip = false
- /* eslint-disable no-new */
-
-
- new Vue({
- el: '#app',
- router,
- data(){
- return{
- Bus:new Vue()
- }
- },
- router,
- components: { App },
- template: '
' - })
- //src/conponents/AppMain.vue
-
"main-container"> -
"asideClass"> -
-
-
-
"main-header"> -
-
-
"main-center"> -
-
-
-
-
- // 导入组件
- import TopNav from '@/components/TopNav.vue'
- import LeftNav from '@/components/LeftNav.vue'
-
- // 导出模块
- export default {
- components: {
- TopNav,
- LeftNav
- },
- data() {
- return {
- asideClass: 'main-aside'
- }
- },
- created() {
- this.$root.Bus.$on('aaa', v => {
- this.asideClass = v ? 'main-aside-collapsed' : 'main-aside';
- })
- }
- };
- .main-container {
- height: 100%;
- width: 100%;
- box-sizing: border-box;
- }
-
- .main-aside-collapsed {
- /* 在CSS中,通过对某一样式声明! important ,可以更改默认的CSS样式优先级规则,使该条样式属性声明具有最高优先级 */
- width: 64px !important;
- height: 100%;
- background-color: #334157;
- margin: 0px;
- }
-
- .main-aside {
- width: 240px !important;
- height: 100%;
- background-color: #334157;
- margin: 0px;
- }
-
- .main-header,
- .main-center {
- padding: 0px;
- border-left: 2px solid #333;
- }
- //src/components/LeftNav.vue
-
default-active="$route.path" default-active="2" class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" - active-text-color="#ffd04b" :collapse="collapsed">
-
- "logobox">
- "logoimg" src="../assets/img/logo.png" alt="">
-
-
-
for="m in menus" :index="'idx_'+m.id" :key="'key_'+m.id"> - "title">
- "m.icon">
- {{m.text}}
-
-
for="m2 in m.modules" :index="m2.url" :key="'key_'+m2.id"> - "m2.icon">
- {{m2.text}}
-
-
-
-
-
- export default {
- data() {
- return {
- collapsed: false,
- menus:[]
- }
- },
- created() {
- this.$root.Bus.$on('aaa', v => {
- this.collapsed = v;
- });
- let url = this.axios.urls.SYSTEM_MENUS;
- this.axios.get(url,{}).then(r => {
- console.log(r);
- this.menus=r.data.rows;
- }).catch(e => {
-
- })
- }
- }
- .el-menu-vertical-demo:not(.el-menu--collapse) {
- width: 240px;
- min-height: 400px;
- }
-
- .el-menu-vertical-demo:not(.el-menu--collapse) {
- border: none;
- text-align: left;
- }
-
- .el-menu-item-group__title {
- padding: 0px;
- }
-
- .el-menu-bg {
- background-color: #1f2d3d !important;
- }
-
- .el-menu {
- border: none;
- }
-
- .logobox {
- height: 40px;
- line-height: 40px;
- color: #9d9d9d;
- font-size: 20px;
- text-align: center;
- padding: 20px 0px;
- }
-
- .logoimg {
- height: 40px;
- }
- //src/components/TopNav.vue
-
-
-
"el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff"> -
"buttonimg"> - "showimg" :src="collapsed?imgshow:imgsq" @click="doToggle()">
-
-
"2" class="submenu"> - "title">超级管理员
-
"2-1">设置 -
"2-2">个人中心 -
@click="exit()" index="2-3">退出 -
-
-
-
- export default {
- data() {
- return {
- collapsed: false,
- imgshow:require('@/assets/img/show.png'),
- imgsq:require('@/assets/img/sq.png')
- }
- },
- methods: {
- doToggle() {
- this.collapsed = !this.collapsed;
- //this.$emit()
- // 将是否折叠放入总线
- this.$root.Bus.$emit('aaa', this.collapsed);
- },
- exit() {
- this.$router.push("/")
- }
- }
- }
-
- .el-menu-vertical-demo:not(.el-menu--collapse) {
- border: none;
- }
-
- .submenu {
- float: right;
- }
-
- .buttonimg {
- height: 60px;
- background-color: transparent;
- border: none;
- }
-
- .showimg {
- width: 26px;
- height: 26px;
- position: absolute;
- top: 17px;
- left: 17px;
- }
-
- .showimg:active {
- border: none;
- }