当前位置:首页 > VUE

vue实现侧部导航

2026-02-18 23:45:44VUE

Vue 实现侧部导航的方法

使用 Vue Router 和动态组件

在 Vue 项目中,可以通过 Vue Router 和动态组件实现侧部导航。创建一个侧边栏组件,结合路由配置动态加载内容。

<template>
  <div class="sidebar">
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' },
        { name: '联系', path: '/contact' }
      ]
    };
  }
};
</script>

<style>
.sidebar {
  width: 200px;
  background: #f4f4f4;
  padding: 20px;
}
.sidebar ul {
  list-style: none;
  padding: 0;
}
.sidebar li {
  margin-bottom: 10px;
}
.sidebar a {
  text-decoration: none;
  color: #333;
}
.sidebar a:hover {
  color: #42b983;
}
</style>

使用 Element UI 的侧边栏组件

如果项目使用 Element UI,可以直接使用其提供的 el-menu 组件实现侧部导航。

<template>
  <el-menu
    default-active="/"
    class="el-menu-vertical"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
    router
  >
    <el-menu-item index="/">
      <i class="el-icon-menu"></i>
      <span>首页</span>
    </el-menu-item>
    <el-menu-item index="/about">
      <i class="el-icon-document"></i>
      <span>关于</span>
    </el-menu-item>
    <el-menu-item index="/contact">
      <i class="el-icon-setting"></i>
      <span>联系</span>
    </el-menu-item>
  </el-menu>
</template>

<script>
export default {
  name: 'Sidebar'
};
</script>

<style>
.el-menu-vertical {
  height: 100vh;
}
</style>

使用 Vuex 管理导航状态

对于需要动态切换导航状态的场景,可以通过 Vuex 管理导航的展开和折叠状态。

// store.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    isSidebarCollapsed: false
  },
  mutations: {
    toggleSidebar(state) {
      state.isSidebarCollapsed = !state.isSidebarCollapsed;
    }
  }
});
<template>
  <div class="sidebar" :class="{ 'collapsed': isSidebarCollapsed }">
    <button @click="toggleSidebar">切换</button>
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapState(['isSidebarCollapsed'])
  },
  methods: {
    ...mapMutations(['toggleSidebar'])
  },
  data() {
    return {
      menuItems: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' },
        { name: '联系', path: '/contact' }
      ]
    };
  }
};
</script>

<style>
.sidebar {
  width: 200px;
  transition: width 0.3s;
}
.sidebar.collapsed {
  width: 60px;
}
</style>

响应式侧边栏

通过监听窗口大小变化,实现侧边栏的响应式布局。

vue实现侧部导航

<template>
  <div class="sidebar" :class="{ 'mobile': isMobile }">
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isMobile: false,
      menuItems: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' },
        { name: '联系', path: '/contact' }
      ]
    };
  },
  mounted() {
    this.checkScreenSize();
    window.addEventListener('resize', this.checkScreenSize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkScreenSize);
  },
  methods: {
    checkScreenSize() {
      this.isMobile = window.innerWidth < 768;
    }
  }
};
</script>

<style>
.sidebar {
  width: 200px;
}
.sidebar.mobile {
  width: 100%;
}
</style>

以上方法可以根据项目需求选择适合的实现方式,灵活调整功能和样式。

标签: vue
分享给朋友:

相关文章

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…