组件封装
在上一篇记录中,首页中有太多的代码,为了避免代码的臃肿,需要对主要的功能模块拆分,来让代码看起来更简洁,且能进行复用。
拆分后还加了些小功能,加入了修改 title 的代码,修改方式参考vue 动态修改 title。
还增加了当前请求的页面缓存,使用状态管理器处理。监听路由,保存到 state 中,来处理的。 如何监听可参考vue 计算属性和监听属性。
完整效果图如下:
首页布局拆分后结构
拆分后的,布局结构图:
拆分后代码
布局最外层 index 代码,使用头部,侧边栏,主内容栏组成,代码如下:
头部 index.vue 代码:
首页 使用文档 GitHub {{ $store.getters.userInfo.username }} 首页 我的主页 登出 .header { padding-left: 0px !important; height: 60px; line-height: 60px; width: 100%; background: #4b5f6e; color: #fff; .heardNavBar { float: left; background: #4b5f6e; padding: 0px 0px; height: 60px; line-height: 60px; font-size: 28px; cursor: pointer; } .userinfo { text-align: right; padding-right: 24px; float: right; padding: 0 10px; .userinfo-inner { font-size: 20px; cursor: pointer; color: #fff; img { width: 40px; height: 40px; border-radius: 10px; margin: 10px 0px 10px 10px; float: right; } } } }
头部中引用的相关组件代码如下
折叠导航栏 hamburger 下的 index.vue 代码:
.hamburger { padding-left: 13px; padding-right: 13px; text-align: center; width: 34px; height: 60px; line-height: 60px; float: left; cursor: pointer; } .is-active { transform: rotate(180deg); }
折叠导航栏 logo 下的 index.vue 代码:
.logo { float: left; height: 60px; padding: 0; margin: 0; } .logo-width { width: 230px; } .logo-collapse-width { width: 65px; }
侧边栏下的 index.vue代码:
系统管理 用户管理 菜单管理 导航一 导航二 .aside-container { position: fixed; top: 0px; left: 0; bottom: 0; z-index: 1020; .el-menu { position: absolute; top: 60px; bottom: 0px; text-align: left; } } .aside-width { width: 230px; } .aside-collapse-width { width: 65px; }
内容模块下的 index.vue代码:
关闭当前标签 关闭其它标签 关闭全部标签 刷新当前标签 {{ item.title }} .main-container { padding: 0 5px 5px; position: absolute; top: 60px; left: 1px; right: 1px; bottom: 0px; .tabs { position: fixed; top: 60px; right: 50px; padding-left: 0px; padding-right: 2px; z-index: 1020; height: 40px; line-height: 40px; font-size: 14px; background: rgb(255, 253, 255); border-color: rgba(200, 206, 206, 0.5); // border-left-width: 1px; // border-left-style: solid; border-bottom-width: 1px; border-bottom-style: solid; } .tabs-tools { position: fixed; top: 60px; right: 0; z-index: 1020; height: 40px; // padding: 0 10px; font-size: 14px; line-height: 40px; cursor: pointer; border-color: rgba(200, 206, 206, 0.5); border-left-width: 1px; border-left-style: solid; border-bottom-width: 1px; border-bottom-style: solid; background: rgba(255, 255, 255, 1); } .tabs-tools:hover { background: rgba(200, 206, 206, 1); } .main-content { position: absolute; top: 45px; left: 5px; right: 5px; bottom: 5px; padding: 5px; // background: rgba(209, 212, 212, 0.5); } } .position-left { left: 230px; } .position-collapse-left { left: 65px; }
状态管理中添加 app 模块
代码如下:
export default {
state: {
// 是否折叠导航栏
isCollapse: false,
// 访问页集合
mainTabs: [],
// 当前访问页名
mainTabsActiveName: '',
},
getters: {
isCollapse: (state) => {
return state.isCollapse
},
},
mutations: {
toggleCollapse(state) {
state.isCollapse = !state.isCollapse
},
updateMainTabs(state, tabs) {
state.mainTabs = tabs
},
updateMainTabsActiveName(state, name) {
state.mainTabsActiveName = name
},
},
actions: {},
}
当然还有一些小的调整点,可参考 git 上的提交版本 首页组件拆分
总结
到此这篇关于Vue管理系统前端之组件拆分封装的文章就介绍到这了,更多相关Vue组件拆分封装内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



