重生之我想写后端

Vue + Element Plus 简单的响应式侧边栏

利用onMounted,初次加载页面的时候,为页面注册一个监听器。 当 'resize' 事件发生时, 触发函数。 此函数里监听窗口大小,当窗口小于 500px 时,改变 isCollpase boolean 值。 实现动态变化。

复制代码
<template>
  <el-menu
    default-active="2"
    class="el-menu-vertical-demo"
    :collapse="isCollapse"
    @open="handleOpen"
    @close="handleClose"
  >
    <el-menu-item index="1">
      <el-icon><Edit /></el-icon>
      <template #title>创建问卷</template>
    </el-menu-item>

    <el-menu-item index="2">
      <el-icon><Document /></el-icon>
      <template #title>全部问卷</template>
    </el-menu-item>

    <el-menu-item index="3">
      <el-icon><Delete /></el-icon>
      <template #title>回收站</template>
    </el-menu-item>

    <el-menu-item index="4">
      <el-icon><DataAnalysis /></el-icon>
      <template #title>数据统计</template>
    </el-menu-item>
  </el-menu>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue';
import {
  Document,
  DataAnalysis,
  Edit ,
  Delete
} from '@element-plus/icons-vue'

const isCollapse = ref(true)
const handleOpen = (key , keyPath) => {
  console.log(key, keyPath)
}
const handleClose = (key, keyPath) => {
  console.log(key, keyPath)
}
// 动态计算宽度 当屏幕分辨率 =< 500px时候使用其他组件
const screenWidth = ref(window.innerWidth);
const handleResize = () => {
  screenWidth.value = window.innerWidth;
  isCollapse.value = screenWidth.value < 500;
  console.log(isCollapse.value);
  console.log(screenWidth.value);
};

onMounted(() => {
  //动态计算宽,调用handleResize方法
  window.addEventListener('resize', handleResize);
});

// 看网上说,避免内存泄漏
onUnmounted(()=>{
  window.removeEventListener('resize',handleResize);
})

</script>

<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>

点击侧边栏,主体区域跟着切换组件

看到网上有好几种实现方式,只用了最简单的一个。

利用 router-view

参考博客

复制代码
<el-menu router > 
    <el-menu-item index="/vote/all">
      <el-icon><Document /></el-icon>
      <template #title>全部问卷</template>
    </el-menu-item>

    <el-menu-item index="/vote/trash">
      <el-icon><Delete /></el-icon>
      <template #title>回收站</template>
    </el-menu-item>

    <el-menu-item index="/vote/analyse">
      <el-icon><DataAnalysis /></el-icon>
      <template #title>数据统计</template>
    </el-menu-item>
</el-menu>

<router-view> < /router-view>

利用Vue 组件实现

代码来自Vue官网

Vue 官网参考

在线运行代码

复制代码
<script>
import Home from './Home.vue'
import Posts from './Posts.vue'
import Archive from './Archive.vue'
  
export default {
  components: {
    Home,
    Posts,
    Archive
  },
  data() {
    return {
      currentTab: 'Home',
      tabs: ['Home', 'Posts', 'Archive']
    }
  }
}
</script>

<template>
  <div class="demo">
    <button
       v-for="tab in tabs"
       :key="tab"
       :class="['tab-button', { active: currentTab === tab }]"
       @click="currentTab = tab"
     >
      {{ tab }}
    </button>
	  <component :is="currentTab" class="tab"></component>
  </div>
</template>

<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}

.tab-button {
  padding: 6px 10px;
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
  border: 1px solid #ccc;
  cursor: pointer;
  background: #f0f0f0;
  margin-bottom: -1px;
  margin-right: -1px;
}
.tab-button:hover {
  background: #e0e0e0;
}
.tab-button.active {
  background: #e0e0e0;
}
.tab {
  border: 1px solid #ccc;
  padding: 10px;
}
</style>
相关推荐
@atweiwei6 小时前
LangChainRust 0.33.0 发布:让 Agent 应用更容易运行、观察和评测
服务器·前端·javascript
yuki蜜语6 小时前
「阴升阳降」不符合物理常识?我用一台热虹吸做了个可拆解的 3D 模型
前端·计算机视觉·前端工程化
ZJPRENO6 小时前
一个被身份证认证逼出来的开源项目:AI 辅助开发实战
前端
GoodStudyAndDayDayUp7 小时前
请求前端页面&traefik流程
前端
AI开源软件10 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
qq_2744751512 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript
GuWenyue14 小时前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度
javascript·数据库·后端
CV-deeplearning14 小时前
Chrome DevTools实战
前端·agent·chrome devtools·mcp
xurileidian14 小时前
第 30 篇 · 边玩边学Python · 猜数字小游戏 把判断循环异常串起来
前端·python·算法
前端精髓14 小时前
三种方式实现 Agent 工具调用:OpenAI、LangChain 与 LangGraph
前端·数据库·langchain