[Vue]实战---电商项目(页面的布局)【三】

主页布局

整体布局
主页布局开始
  • 引入官网的框架时,首先要在element.js中添加
import { 
    Container,
    Header,
    Aside,
    Main
} from 'element-ui'
Vue.use(Container)
Vue.use(Header)
Vue.use(Aside)
Vue.use(Main)
<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="Login.jpg" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}
</style>

 

左侧菜单布局
  • 引入官网的框架时,首先要在element.js中添加
import { 
    Menu,
    Submenu,
    MenuItemGroup,
    MenuItem
} from 'element-ui'
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItemGroup)
Vue.use(MenuItem)
  • 渲染左侧菜单
<!-- 侧边栏 -->
<el-aside :width="200px">
    <!-- 侧边栏菜单区域 -->
    <!-- unique-opened:每次只展开一个子菜单 -->
    <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
        <!-- 一级菜单 -->
        <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
<!-- 图标 -->
<i :class="iconsObj[item.id]"></i>
<!-- 文本 -->
<span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                <template slot="title">
<!-- 图标 -->
<i class="el-icon-menu"></i>
<!-- 文本 -->
<span>{{subItem.authName}}</span>
                </template>
            </el-menu-item>
        </el-submenu>
    </el-menu>
</el-aside>

 

通过接口获取菜单数据

通过axios请求拦截器添加token,保证拥有获取数据的权限

  • main.js文件中添加
// axios请求拦截
axios.interceptors.request.use(config => {
    // 为请求头对象,添加 Token 验证的 Authorization 字段
    config.headers.Authorization = window.sessionStorage.getItem('token')
    return config
})

 

优化左侧菜单
  • 获取到所有菜单
created() {
    //获取到所有菜单
    this.getMenuList()
},
    methods: {
        async getMenuList() {
            const {data: res} = await this.$http.get('menus')
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.menulist = res.data
            console.log(res)
        },
    }

 

  • 实现图标出现
data() {
    return {
        // 左侧菜单数据
        menulist: [],
        iconsObj: {
            '125': 'iconfont icon-user',
            '103': 'iconfont icon-tijikongjian',
            '101': 'iconfont icon-shangpin',
            '102': 'iconfont icon-danju',
            '145': 'iconfont icon-baobiao'
        },
    }
}

 

  • 实现文本与图标的间隙
.iconfont {
    margin-right: 10px;
}

 

  • 实现左侧菜单折叠和展开

    • 在基本样式中添加
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
    </el-aside>
    
    • 在css样式中添加
    .toggle-button {
        background-color: #4a5064;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
    
    • 点击按钮,切换菜单的折叠与展开
    <script>
        export default {
    		data() {
        		return {
    
    				// 是否折叠
    				isCollapse: false,
        		}
    		},
    		methods: {
    			toggleCollapse() {
    				this.isCollapse = !this.isCollapse
    			}
        	}
    	}
    </script>
    

 

路由重定向
  • 创建新的Welcome.vue
  • index.js中添加
import Welcome from "../components/Welcome";
const routes = [
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children:[
            {
                path: '/welcome',
                component: Welcome
            }
        ]
    }

]
  • 在右侧内容主体区域添加路由占位符
<el-main>
    <!-- 路由占位符 -->
    <router-view></router-view>
</el-main>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值