SpringBoot2+Vue2实战(四)进行组件内容拆分及路由实现

一、拆分

新建包:

Aside和Header都是组件

User为视图

 Aside.vue:

<template><el-menu :default-openeds="['1', '3']" style="min-height: 100%; overflow-x: hidden"background-color="rgb(48, 65, 86)"text-color="#fff"active-text-color="#ffd04b":collapse-transition="false":collapse="isCollapse"><div style="height: 60px; line-height: 60px; text-align: center"><img src="../assets/logo.png" alt="" style="width: 20px; position: relative; top: 5px; right: 5px"><b style="color: white" v-show="logoTextShow">后台管理系统</b></div><el-menu-item index="/"><template slot="title"><i class="el-icon-s-home"></i><span slot="title">主页</span></template></el-menu-item><el-submenu index="2"><template slot="title"><i class="el-icon-menu"></i><span slot="title">系统管理</span></template><el-menu-item index="/user"><template slot="title"><i class="el-icon-user"></i><span slot="title">用户管理</span></template></el-menu-item></el-submenu></el-menu>
</template><script>
export default {name: "Aside",//根据数据类型定义变量props:{isCollapse:Boolean,logoTextShow:Boolean},}
</script><style scoped></style>

Head.vue

<template><div style="font-size: 12px;line-height: 60px; display: flex"><div style="flex: 1;"><span :class="collapseBtnClass" style="cursor: pointer; font-size: 18px" @click="collapse"></span><el-breadcrumb separator="/" style="display: inline-block;margin-left: 10px" ><el-breadcrumb-item :to="item.paths" :key v-for="item in paths">{{ item.name }}</el-breadcrumb-item></el-breadcrumb></div><el-dropdown style="width: 70px; cursor: pointer"><span>王小虎</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i><el-dropdown-menu slot="dropdown"><el-dropdown-item style="font-size: 14px; padding: 5px 0">个人信息</el-dropdown-item><el-dropdown-item style="font-size: 14px; padding: 5px 0">退出</el-dropdown-item></el-dropdown-menu></el-dropdown></div>
</template><script>
export default {name: "Header",props:{collapseBtnClass: String,collapse:Function},data(){return{paths:[]}},
}
</script><style scoped></style>

User.vue

拆出页面布局及方法

<template>
<div><div style="margin: 10px 0"><el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"v-model="username"></el-input><el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message"v-model="email" class="ml-5"></el-input><el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position"v-model="address" class="ml-5"></el-input><el-button class="ml-5" type="primary" @click="load">搜索</el-button><el-button class="ml-5" type="warning" @click="reset">重置</el-button></div><div style="margin: 10px 0"><el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button><el-popconfirmclass="ml-5"confirm-button-text='确定'cancel-button-text='我再想想'icon="el-icon-info"icon-color="red"title="这是一段内容确定删除吗?"@confirm="delBatch"><el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button></el-popconfirm><el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button><el-button type="primary">导出 <i class="el-icon-top"></i></el-button></div><el-table :data="tableData" border stripe :header-cell-class-name="headerBg"@selection-change="handleSelectionChange"><el-table-columntype="selection"width="55"></el-table-column><el-table-column prop="id" label="ID" width="80"></el-table-column><el-table-column prop="username" label="用户名" width="140"></el-table-column><el-table-column prop="nickname" label="昵称" width="120"></el-table-column><el-table-column prop="email" label="邮箱"></el-table-column><el-table-column prop="phone" label="电话"></el-table-column><el-table-column prop="address" label="地址"></el-table-column><el-table-column label="操作" width="200" align="center"><template slot-scope="scope"><el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button><el-popconfirmconfirm-button-text='确定'cancel-button-text='我再想想'icon="el-icon-info"icon-color="red"title="这是一段内容确定删除吗?"@confirm="del(scope.row.id)"><el-button type="danger" slot="reference" class="ml-5">删除 <i class="el-icon-remove-outline"></i></el-button></el-popconfirm></template></el-table-column></el-table><div style="padding: 10px 0"><el-pagination@size-change="handleSizeChange"@current-change="handleCurrentChange":current-page="pageNum":page-sizes="[2, 5, 10, 20]":page-size="pageSize"layout="total, sizes, prev, pager, next, jumper":total="total"></el-pagination></div><!--新增表单--><el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%"><el-form label-width="80px" size="small"><el-form-item label="用户名"><el-input v-model="form.username" autocomplete="off"></el-input></el-form-item><el-form-item label="昵称"><el-input v-model="form.nickname" autocomplete="off"></el-input></el-form-item><el-form-item label="邮箱"><el-input v-model="form.email" autocomplete="off"></el-input></el-form-item><el-form-item label="电话"><el-input v-model="form.phone" autocomplete="off"></el-input></el-form-item><el-form-item label="地址"><el-input v-model="form.address" autocomplete="off"></el-input></el-form-item></el-form><div slot="footer" class="dialog-footer"><el-button @click="dialogFormVisible = false">取 消</el-button><el-button type="primary" @click="save">确 定</el-button></div></el-dialog>
</div>
</template><script>
export default {name: "User",data(){return {dialogFormVisible: false,multipleSelection: [],tableData: [],total: 0,pageNum: 1,pageSize: 2,form: {},username: "",email: "",address: "",headerBg: 'headerBg'}},created() {this.load()},methods:{load() {this.request.get("/user/selectPage", {params: {pageNum: this.pageNum,pageSize: this.pageSize,username: this.username,address: this.address,email: this.email}}).then(res => {console.log(res)this.tableData = res.recordsthis.total = res.total})},reset() {this.username = ""this.email = ""this.address = ""},//新增方法handleAdd() {this.dialogFormVisible = truethis.form = {}},save() {this.request.post("/user/saveUser", this.form).then(res => {if (res) {this.$message.success("保存成功")this.dialogFormVisible = falsethis.load()} else {this.$message.error("保存失败")this.dialogFormVisible = false}})},/*编辑方法*/handleEdit(row) {this.form = JSON.parse(JSON.stringify(row))this.dialogFormVisible = true},//删除方法del(id) {this.request.delete("/user/" + id).then(res => {if (res) {this.$message.success("删除成功")this.load()} else {this.$message.error("删除失败")}})},//批量删除delBatch() {//把对象的数组变成纯id数组let ids = this.multipleSelection.map(v => v.id)this.request.post("/user/del/batch", ids).then(res => {if (res) {this.$message.success("删除成功")this.load()} else {this.$message.error("删除失败")}})},handleSelectionChange(val) {console.log(val)this.multipleSelection = val},handleSizeChange(pageSize) {console.log(pageSize)this.pageSize = pageSizethis.load()},handleCurrentChange(pageNum) {console.log(pageNum)this.pageNum = pageNumthis.load()}}
}
</script><style>
.headerBg {background: #eee !important;
}
</style>

Manage.vue

调用拆分的视图及组件

视图:使用router-view调用

组件:导入组件包:

//导入
import Aside from "@/components/Aside.vue";
import Header from "@/components/Header.vue";

创建组件包含内容

//调用组件components: {Aside,Header,},

调用组件:

<el-aside :width="sideWidth + 'px'" style="box-shadow: 2px 0 6px rgb(0 21 41 / 35%);"><Aside :isCollapse="isCollapse" :logoTextShow="logoTextShow"/></el-aside><el-container><el-header style=" border-bottom: 1px solid #ccc;"><Header :collapseBtnClass="collapseBtnClass" :collapse="collapse"/></el-header><el-main><!--表示当前页面的子路由会在router-view展示--><router-view/></el-main></el-container>
<template><el-container style="min-height: 100vh"><el-aside :width="sideWidth + 'px'" style="box-shadow: 2px 0 6px rgb(0 21 41 / 35%);"><Aside :isCollapse="isCollapse" :logoTextShow="logoTextShow"/></el-aside><el-container><el-header style=" border-bottom: 1px solid #ccc;"><Header :collapseBtnClass="collapseBtnClass" :collapse="collapse"/></el-header><el-main><!--表示当前页面的子路由会在router-view展示--><router-view/></el-main></el-container></el-container>
</template><script>
//导入
import Aside from "@/components/Aside.vue";
import Header from "@/components/Header.vue";export default {name: 'Home',data() {return {collapseBtnClass: 'el-icon-s-fold',isCollapse: false,sideWidth: 200,logoTextShow: true,}},//调用组件components: {Aside,Header,},methods: {collapse() {  // 点击收缩按钮触发this.isCollapse = !this.isCollapsethis.logoTextShow = !this.logoTextShowif (this.isCollapse) {  // 收缩this.sideWidth = 64this.collapseBtnClass = 'el-icon-s-unfold'this.logoTextShow = false} else {   // 展开this.sideWidth = 200this.collapseBtnClass = 'el-icon-s-fold'this.logoTextShow = true}},}
}
</script><style>
</style>

router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'Vue.use(VueRouter)const routes = [{path: '/',component: () => import('../views/Manage.vue'),redirect:"/home",children:[{path: 'user', name: '用户管理', component: () => import('../views/User.vue'),},{path: 'home', name: '首页', component: () => import('../views/Home.vue'),}]},{path: '/about',name: 'About',component: () => import('../views/AboutView.vue')}
]const router = new VueRouter({mode: 'history',base: process.env.BASE_URL,routes
})export default router

Vue路由使用:

Aside:

<template><el-menu :default-openeds="['1', '3']" style="min-height: 100%; overflow-x: hidden"background-color="rgb(48, 65, 86)"text-color="#fff"active-text-color="#ffd04b":collapse-transition="false":collapse="isCollapse"
//添加就能跳转router><div style="height: 60px; line-height: 60px; text-align: center"><img src="../assets/logo.png" alt="" style="width: 20px; position: relative; top: 5px; right: 5px"><b style="color: white" v-show="logoTextShow">后台管理系统</b></div><el-menu-item index="/home"><template slot="title"><i class="el-icon-house"></i><span slot="title">主页</span></template></el-menu-item><el-submenu index="2"><template slot="title"><i class="el-icon-menu"></i><span slot="title">系统管理</span></template><el-menu-item index="/user"><i class="el-icon-s-custom"></i><span slot="title">用户管理</span></el-menu-item></el-submenu></el-menu>
</template><script>
export default {name: "Aside",props: {isCollapse: Boolean,logoTextShow: Boolean}
}
</script><style scoped></style>

router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'Vue.use(VueRouter)const routes = [{path: '/',component: () => import('../views/Manage.vue'),redirect:"/home",children:[{path: 'user', name: '用户管理', component: () => import('../views/User.vue'),},{path: 'home', name: '首页', component: () => import('../views/Home.vue'),}]},{path: '/about',name: 'About',component: () => import('../views/AboutView.vue')}
]const router = new VueRouter({mode: 'history',base: process.env.BASE_URL,routes
})/*// 路由守卫
router.beforeEach((to, from, next) => {localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称,为了在Header组件中去使用store.commit("setPath")  // 触发store的数据更新next()  // 放行路由
})*/export default router

Head.vue

<template><div style="line-height: 60px; display: flex"><div style="flex: 1;"><span :class="collapseBtnClass" style="cursor: pointer; font-size: 18px" @click="collapse"></span><el-breadcrumb separator="/" style="display: inline-block; margin-left: 10px"><el-breadcrumb-item v-if="this.$route.name !== '主页'">{{ this.$route.name }}</el-breadcrumb-item></el-breadcrumb></div><el-dropdown style="width: 70px; cursor: pointer"><span>王小虎</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i><el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center"><el-dropdown-item style="font-size: 14px; padding: 5px 0">个人信息</el-dropdown-item><el-dropdown-item style="font-size: 14px; padding: 5px 0">退出</el-dropdown-item></el-dropdown-menu></el-dropdown></div>
</template><script>
export default {name: "Header",props: {collapseBtnClass: String,collapse: Function,},/*computed: {currentPathName () {return this.$store.state.currentPathName;  //需要监听的数据}}*/
}
</script><style scoped></style>

Vuex下载

npm i vuex -S

store.js

import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)const store = new Vuex.Store({state: {currentPathName: ''},mutations: {setPath (state) {state.currentPathName = localStorage.getItem("currentPathName")}}
})export default store

main.js

import store from './store'new Vue({router,store,render: h => h(App)
}).$mount('#app')

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/854.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Flume】高级组件之Sink Processors及项目实践(Sink负载均衡和故障转移)

文章目录 1. 组件简介2. 项目实践2.1 负载均衡2.1.1 需求2.1.2 配置2.1.3 运行 2.2 故障转移2.2.1 需求2.2.2 配置2.2.3 运行 1. 组件简介 Sink Processors类型包括这三种&#xff1a;Default Sink Processor、Load balancing Sink Processor和Failover Sink Processor。 Defa…

供应商索赔(金税数据)导入并创建凭证(ALV长篇备忘三)

情境/背景:供应商三包索赔款项源起QMS质量系统&#xff0c;联动金税系统完成发票开具&#xff0c;最终在SAP系统中创建完成财务凭证。该流程为手工操作&#xff0c;费时费力且效率低下容易出错。 目标/任务:把QMS供应商三包索赔业务搬上线,同SAP FI顾问梳理功能说明书&#xf…

星辰秘典:揭开Python项目的神秘密码——2048游戏

✨博主&#xff1a;命运之光 &#x1f338;专栏&#xff1a;Python星辰秘典 &#x1f433;专栏&#xff1a;web开发&#xff08;html css js&#xff09; ❤️专栏&#xff1a;Java经典程序设计 ☀️博主的其他文章&#xff1a;点击进入博主的主页 前言&#xff1a;你好&#x…

一步一步学OAK之七:通过OAK相机实现特征跟踪

目录 特征跟踪Setup 1: 创建文件Setup 2: 安装依赖Setup 3: 导入需要的包Setup 4: 定义FeatureTrackerDrawer类定义变量定义onTrackBar方法定义trackFeaturePath方法定义drawFeatures方法定义FeatureTrackerDrawer类的构造函数 Setup 5: 创建pipelineSetup 6: 创建节点创建相机…

GIT保存记录原理之commit对象

GIT 中提交对象非常的重要&#xff0c;我们通过它记录代码提交过程、进行文件保存、回退等操作&#xff0c;那么它是怎样帮助我们记录这些信息的呢&#xff1f;其实就是都保存在项目根目录的 .git 文件夹中。 新建空项目 gitDemo使用 git init初始化&#xff0c;在文件夹根目录…

图像去模糊:RSBlur 数据集以及模糊图像合成方法

本内容主要介绍图像去模糊数据集 RSBlur&#xff0c;以及逼真模糊图像合成方法。 论文&#xff1a;Realistic Blur Synthesis for Learning Image Deblurring 代码&#xff08;官方&#xff09;&#xff1a;https://github.com/rimchang/RSBlur 1.1 背景 运动模糊是由曝光…

vue 组件简单实例及传参交互

前言:vue 可以比较灵活的使用 html的片段&#xff0c;并将html的片段进行数据隔离&#xff0c;参数也可以互相传递&#xff0c;组件与组件之间也可以进行数据的交互 合理的使用组件可以避免重复代码或者很方便的调用第三方组件库 vue组件 简单实例组件传参实际应用父子组件交互…

maven打包所有依赖,对外提供sdk.jar

maven打包所有依赖 <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compile.source>1.8</maven.compile.source><maven.compile.target>1.8</maven.compile.target></properties><…

分布式计算模型详解:MapReduce、数据流、P2P、RPC、Agent

前言 本文隶属于专栏《大数据理论体系》&#xff0c;该专栏为笔者原创&#xff0c;引用请注明来源&#xff0c;不足和错误之处请在评论区帮忙指出&#xff0c;谢谢&#xff01; 本专栏目录结构和参考文献请见大数据理论体系 思维导图 MapReduce MapReduce 是一种分布式计算模…

在 Maya、ZBrush、Substance 3D 和 UE5 中创建理发椅

今天瑞云渲染小编给大家带来Kevin J. Coulman 分享的理发椅项目背后的工作流程&#xff0c;详细介绍了如何在 Maya 和 ZBrush 中为道具建模&#xff0c;分享了制作准确材质的技巧&#xff0c;并解释了为什么选择 UE5 进行渲染。 介绍 大家好! 我的名字是Mehdi Benmansour&…

layui实现选择框搜索(下拉搜索)功能

1.可以使用官方介绍的方法&#xff0c;适用于form表单内的下拉搜索&#xff0c;外层需要使用layui-form样式&#xff0c;select标签内添加lay-search“”&#xff0c;此方法若外层不添加layui-form无法实现搜索功能&#xff0c;如下所示&#xff1a; 2.下面是另一种形式的下拉选…

设计一个高流量高并发的系统需要关注哪些点

1、设计原则 1.1、系统设计原则 在设计一个系统之前&#xff0c;我们先要有一个统一且清晰的认知&#xff1a;不要想着一下就能设计出完美的系统&#xff0c;好的系统是迭代出来的。不要复杂化&#xff0c;要先解决核心问题。但是要有先行的规划&#xff0c;对现有的问题有方…