重生之我想写后端

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>
相关推荐
崔庆才丨静觅5 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60615 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了5 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅5 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅6 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅6 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment6 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅7 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊7 小时前
jwt介绍
前端
爱敲代码的小鱼7 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax