<template> <div class="sidebar"> <el-row class="menu_page"> <el-col> <el-menu mode="vertical" class="el-menu-vertical-demo" :collapse="collapse" :default-active="$route.path" background-color="#324057" text-color="#fff"> <div class="wrapper" ref="wrapper"> <Menu :items='items'/> </div> </el-menu> </el-col> </el-row> </div> </template>
时间: 2023-10-21 12:05:34 浏览: 165
这是一个 Vue.js 的组件代码,该组件定义了一个名为 "sidebar" 的侧边栏,它包含一个 el-menu 组件,其中的 items 数据由上层组件传递进来,根据传入的数据动态生成菜单项。其中,el-menu 组件的 mode 属性设置为 vertical,表示垂直布局,collapse 属性用于指定侧边栏是否折叠,default-active 属性用于设置默认选中项,background-color 和 text-color 属性用于设置菜单的背景色和文本颜色。此外还有一个名为 "wrapper" 的 div 元素,用于包裹 el-menu 组件。
相关问题
<template> <div class="home"> <el-row class="header"> <el-col :span="24"> <!-- 头部内容 --> <h1>后台管理系统</h1> <a href="#" @click="handleLogout">注销</a> </el-col> </el-row> <el-row class="content"> <el-col :span="6" class="sidebar"> <!-- 侧边栏 --> <el-menu :default-active="activeMenu" class="menu"> <el-menu-item index="1"> <i class="el-icon-s-home"></i> <span>首页</span> </el-menu-item> <el-menu-item index="/category"> <i class="el-icon-s-data"></i> <span>分类管理</span> <router-link to="/category"> <el-menu-item>分类管理</el-menu-item> </router-link> </el-menu-item> <el-menu-item index="3"> <i class="el-icon-s-tools"></i> <span>系统设置</span> </el-menu-item> </el-menu> </el-col> <el-col :span="18" class="main"> <!-- 主要内容 --> <router-view ></router-view> </el-col> </el-row> <el-row class="footer"> <el-col :span="24"> <!-- 底部信息 --> 版权所有 © 2022 后台管理系统 </el-col> </el-row> </div> </template>还是会跳到新页面
根据你提供的代码,问题出现在你在 `<router-link>` 标签内嵌套了 `<el-menu-item>` 标签。这样会导致 `<el-menu-item>` 标签成为嵌套关系中的子菜单项,而不是路由链接。
为了解决这个问题,你应该将 `<router-link>` 标签直接放在 `<el-menu-item>` 标签的外层。这样,点击菜单项时才会触发路由切换。
以下是修改后的示例代码:
```html
<el-menu :default-active="activeMenu" class="menu">
<el-menu-item index="1">
<router-link to="/">
<i class="el-icon-s-home"></i>
<span>首页</span>
</router-link>
</el-menu-item>
<el-menu-item index="/category">
<router-link to="/category">
<i class="el-icon-s-data"></i>
<span>分类管理</span>
</router-link>
</el-menu-item>
<el-menu-item index="3">
<router-link to="/system">
<i class="el-icon-s-tools"></i>
<span>系统设置</span>
</router-link>
</el-menu-item>
</el-menu>
```
在这个修改后的代码中,`<router-link>` 标签直接包裹菜单项的图标和文字内容。这样,在点击菜单项时,会触发路由切换,而不会跳转到新页面。
希望这次的回答能够解决你的问题。如果还有其他疑问,请随时提问。
<template> <div> <el-menu mode="vertical" class="sidebar"> <el-menu-item index="1">学校介绍</el-menu-item> <el-menu-item index="2" @click="show = 1">学校地图</el-menu-item> <el-menu-item index="3">专业信息</el-menu-item> </el-menu> </div> <div v-if="show === 1" class="main"> <img :src="mapImage" alt="学校地图" /> </div> </template> 把图片容器放到el-menu的右侧
你可以将图片容器放到 `<el-menu>` 的右侧,可以通过在 `<el-menu>` 外部包裹一个 `<el-row>`,然后在 `<el-row>` 中创建两个列,一个用于放置 `<el-menu>`,另一个用于放置图片容器。以下是修改后的代码示例:
```html
<template>
<div>
<el-row>
<el-col :span="16">
<el-menu mode="vertical" class="sidebar">
<el-menu-item index="1">学校介绍</el-menu-item>
<el-menu-item index="2" @click="show = 1">学校地图</el-menu-item>
<el-menu-item index="3">专业信息</el-menu-item>
</el-menu>
</el-col>
<el-col :span="8">
<div v-if="show === 1" class="main">
<img :src="mapImage" alt="学校地图" />
</div>
</el-col>
</el-row>
</div>
</template>
```
在上面的代码中,我们使用了 `<el-row>` 和 `<el-col>` 来创建两列布局,左侧列占据了 16 格(即 `:span="16"`),右侧列占据了 8 格(即 `:span="8"`)。左侧列包含了原来的 `<el-menu>`,右侧列包含了图片容器。这样就能实现将图片容器放到 `<el-menu>` 的右侧。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
目录 菜系管理 菜谱管理 菜系列表 <button onclick="addCuisine()" class="btn btn1 btn1">添加</button> <button onclick="updateCuisine()" class="btn btn2 btn2"> 修改</button> <button onclick="deleteCuisineById()" class="btn btn3 btn3">删除</button> <input type="text" name="name" id="name" style="width: 10ch" class="inputText"> <button onclick="searchCuisineByName()" class="btn btn4 btn4">按姓名查询</button>
序号 菜名 所属地 口味 <input type="radio" th:value="${cuisine.id}" name="rid"> <form id="hiddenForm" method="post" th:action="@{/toUpdateCuisine}" hidden> <input type="text" name="id" id="formid"> </form> 美化页面导航条
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
优化下面的代码,解决数据显示区域被侧边栏挡住部分的情况,然后解决大模块展开后与第一个子模块在一起的情况<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>后台管理界面示例</title> <style> body { margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } header { background-color: #333; color: #fff; display: flex; justify-content: space-between; align-items: center; padding: 20px 20px 20px 20px; position: fixed; top: 0; left: 0; right: 0; z-index: 1; } header h1 { margin: 0; font-size: 24px; } header .user { display: flex; align-items: center; cursor: pointer; } header .user img { width: 30px; height: 30px; border-radius: 50%; margin-right: 10px; } .sidebar { background-color: #eee; position: fixed; top: 70px; left: 0; bottom: 0; width: 200px; padding: 10px; overflow: auto; z-index: 1; } .sidebar h2 { margin: 0; font-size: 18px; margin-bottom: 10px; } .sidebar ul { padding: 0; margin: 0; list-style: none; } .sidebar li { margin-bottom: 5px; } .sidebar a { display: block; padding: 5px 10px; color: #333; border-radius: 5px; text-decoration: none; background-color: #fff; transition: background-color 0.2s ease-in-out; } .sidebar a:hover { background-color: #ddd; } .content { margin: 60px 0 0 20px; padding: 10px; background-color: #f5f5f5; min-height: 100vh; margin-left: 200px; } </style> </head> <body> 中央管理平台
未登录 功能模块 用户管理 添加用户 删除用户 修改用户 数据库管理 备份数据库 还原数据库 页面管理 添加页面 删除页面 修改页面 <script> // 模拟后端数据传来时,只刷新头部和侧边栏之外的区域 setTimeout(() => { document.querySelector('.content').innerHTML = '欢迎使用中央管理平台这里是内容区域,只有在后端有数据传来时才会刷新。'; }, 3000); </script> </body> </html>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
优化下面的代码,要求固定侧边栏和头部,解决侧边栏遮挡内容显示区域的情况<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>后台管理界面示例</title> <style> body { margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } header { background-color: #333; color: #fff; display: flex; justify-content: space-between; align-items: center; padding: 20px 20px 20px 20px; position: fixed; top: 0; left: 0; right: 0; z-index: 1; } header h1 { margin: 0; font-size: 24px; } header .user { display: flex; align-items: center; cursor: pointer; } header .user img { width: 30px; height: 30px; border-radius: 50%; margin-right: 10px; } .sidebar { background-color: #eee; position: absolute; top: 70px; left: -200px; bottom: 0; width: 200px; padding: 10px; overflow: auto; z-index: 1; transition: left 0.3s ease-in-out; } .sidebar.show { left: 0; } .sidebar h2 { margin: 0; font-size: 18px; margin-bottom: 10px; } .sidebar ul { padding: 0; margin: 0; list-style: none; } .sidebar li { margin-bottom: 5px; } .sidebar a { display: block; padding: 5px 10px; color: #333; border-radius: 5px; text-decoration: none; background-color: #fff; transition: background-color 0.2s ease-in-out; } .sidebar a:hover { background-color: #ddd; } .content { margin: 60px 0 0 220px; padding: 10px; background-color: #f5f5f5; min-height: calc(100vh - 70px - 10px); } </style> </head> <body> 中央管理平台
未登录 功能模块 用户管理 添加用户 删除用户 修改用户 数据库管理 备份数据库 还原数据库 页面管理 添加页面 删除页面 修改页面 <script> // 动态计算内容区域的左边距,避免与侧边栏重合 function adjustContentMargin() { const sidebarWidth = document.querySelector('.sidebar').offsetWidth; document.querySelector('.content').style.marginLeft = sidebarWidth + 'px'; } adjustContentMargin(); window.addEventListener('resize', adjustContentMargin); // 点击菜单按钮时显示/隐藏侧边栏 document.querySelector('.menu-btn').addEventListener('click', () => { document.querySelector('.sidebar').classList.toggle('show'); }); // 模拟后端数据传来时,只刷新头部和侧边栏之外的区域 setTimeout(() => { document.querySelector('.content').innerHTML = '欢迎使用中央管理平台这里是内容区域,只有在后端有数据传来时才会刷新。'; adjustContentMargin(); // 内容区域高度可能会变化,需要重新计算左边距 }, 3000); </script> </body> </html>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)