1. AppMain局部滚动,可以固定住表头等重要信息
src/layout/components/AppMain.vue
.app-main {
/*50 = navbar */
height: calc(100vh - 70px);
calc(100% - 20px);
position: relative;
overflow: auto;
background: #ffffff;
margin: 10px;
}
//外部
.main-container {
background: #f2f2f2;
}
使用
<template>
<div class='main></div>
</template>
<script>
//每个table页最外层
.main {
height: 100%;
}
</script>
2.显示三级菜单
src/components/Breadcrub/index.vue
getBreadcrumb() {
// only show routes with meta.title
let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
const first = matched[0]
//注释掉首页拼接
// if (!this.isDashboard(first)) {
// matched = [{ path: '/dashboard', meta: { title: 'Dashboard' }}].concat(matched)
// }
this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
},
src/router/index.js
{
path: "/example",
component: Layout,
redirect: "noRedirect",
name: "example",
meta: { title: "一级页面" },
children: [
{
path: "/example/table",
name: "Table",
component: () => import("@/views/table/index"),
activeMenu: "/example",
meta: {
title: "二级页面",
showRole: true, //二级页面必须填写
keepAlive: true,//保持状态必须填写
},
children: [
{
path: "/example/table/tree",
name: "三级页面",
component: () => import("@/views/tree/index"),
meta: { title: "Tree" },
activeMenu: "/example/table",
hidden: true
}
]
}
]
},
使用 二级页面
<template>
<div class='main>
<router-view></router-view>
<div class='main v-if="$route.meta.showRole">
</div
</div>
</template>
mounted() {
if (this.$route.meta.showRole) {
this.getList();
}
},
<script>
//每个table页最外层
.main {
height: 100%;
}
</script>
3.侧边栏只开一个
src/layout/components/Sidebar/index.vue
//:unique-opened="true"
<div :class="{ 'has-logo': showLogo }">
<logo v-if="showLogo" :collapse="isCollapse" />
<el-scrollbar wrap-class="scrollbar-wrapper">
<el-menu :default-active="activeMenu" :collapse="isCollapse" :background-color="variables.menuBg" :text-color="variables.menuText" :unique-opened="true" :active-text-color="variables.menuActiveText" :collapse-transition="false" mode="vertical">
<sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
</el-menu>
</el-scrollbar>
</div>
4.主体页面loading
为了加强控制,将loading写到store里
src/store/modules/app.js
const state = {
loading: false
};
const mutations = {
CHANGE_LOADING: (state, status) => {
state.loading = status;
}
};
const actions = {
changeLoading({ commit }, status) {
commit("CHANGE_LOADING", status);
}
};
src/store/getters.js
const getters = {
loading: state => state.app.loading,
}
export default getters
src/layout/components/AppMain.vue
<template>
<section class="app-main" v-loading="loading">
<transition name="fade-transform" mode="out-in">
<router-view :key="key" />
</transition>
</section>
</template>
<script>
export default {
name: 'AppMain',
computed: {
key() {
//切换路由,loading关闭
this.$store.dispatch('app/changeLoading', false);
//5秒则请求超时,超过6秒则关闭loading
let that = this;
setTimeout(function () {
that.$store.dispatch('app/changeLoading', false);
}, 6000);
return this.$route.path;
},
loading() {
let loading = this.$store.state.app.loading;
return loading;
},
},
};
</script>
5.keepAlive
src/layout/components/AppMain.vue
<template>
<section class="app-main" v-loading="loading">
<transition name="fade-transform" mode="out-in">
<keep-alive v-if="$route.meta.keepAlive">
<router-view :key="key" />
</keep-alive>
<router-view :key="key" v-else />
</transition>
<!-- 全局回到顶部 -->
<el-backtop target=".app-main" :visibility-height="100"></el-backtop>
</section>
</template>
src/router/index.js
{
path: "/example",
component: Layout,
redirect: "noRedirect",
name: "example",
meta: { title: "一级页面" },
children: [
{
path: "/example/table",
name: "Table",
component: () => import("@/views/table/index"),
activeMenu: "/example",
meta: {
title: "二级页面",
showRole: true, //二级页面必须填写
keepAlive: true,//保持状态必须填写
},
children: [
{
path: "/example/table/tree",
name: "三级页面",
component: () => import("@/views/tree/index"),
meta: { title: "Tree" },
activeMenu: "/example/table",
hidden: true
}
]
}
]
},
6. 关闭严格化
vue.config.js
// lintOnSave: process.env.NODE_ENV === "development",
lintOnSave: process.env.NODE_ENV === "false",