uniapp中制作侧边导航栏

制作一个简单的侧边导航栏,并且不使用额外的组件库,可以使用uni-app提供的基础组件和样式来实现。以下是一个使用uni-app基础组件和样式制作的简单侧边导航栏示例:

<template>
  <view class="sidebar-container">
    <view class="sidebar">
      <view class="sidebar-header">
        Logo
      </view>
      <view class="sidebar-menu">
        <view class="sidebar-item" v-for="(item, index) in sidebarItems" :key="index" @click="handleSidebarItemClick(item)">
          {{ item.title }}
        </view>
      </view>
    </view>
    <view class="main-content">
      <!-- 主要内容区域 -->
      <!-- 这里放主要内容 -->
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      sidebarItems: [
        { title: '菜单项1' },
        { title: '菜单项2' },
        { title: '菜单项3' },
        { title: '菜单项4' }
      ]
    };
  },
  methods: {
    handleSidebarItemClick(item) {
      // 处理侧边栏菜单项点击事件
      console.log('点击了菜单项:', item);
    }
  }
};
</script>

<style>
.sidebar-container {
  display: flex;
}

.sidebar {
  background-color: #f0f0f0;
  width: 200px;
}

.sidebar-header {
  padding: 20px;
  font-weight: bold;
  border-bottom: 1px solid #ccc;
}

.sidebar-menu {
  margin-top: 20px;
}

.sidebar-item {
  padding: 10px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.sidebar-item:hover {
  background-color: #e0e0e0;
}

.main-content {
  flex: 1;
  padding: 20px;
}
</style>

在这个示例中,我们使用了uni-app提供的基础组件和样式来制作侧边导航栏。`.sidebar-container`是一个Flex容器,其中包含了`.sidebar`和`.main-content`两个子元素。

`.sidebar`是侧边导航栏的容器,具有固定的宽度和背景色。`.sidebar-header`是导航栏的标题部分,`.sidebar-menu`是菜单项的容器。

菜单项使用`v-for`指令动态渲染,通过点击菜单项触发`handleSidebarItemClick`方法处理点击事件。

`.sidebar-item`定义了菜单项的样式,设置了鼠标悬浮时的背景色变化效果。

`.main-content`是主要内容区域,具有flex-grow属性,使其自动填充剩余空间。

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现侧边分类导航栏,您需要执行以下步骤: 1.创建一个分类表 您需要创建一个分类表,该表包含分类的名称和对应的ID。您可以使用数据库软件(如MySQL)来创建此表。如下所示: CREATE TABLE categories ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, PRIMARY KEY (id) ); 2.添加分类数据 在分类表添加分类数据。如下所示: INSERT INTO categories (name) VALUES ('电子产品'); INSERT INTO categories (name) VALUES ('服装'); INSERT INTO categories (name) VALUES ('家居用品'); 3.在Vue获取分类数据 使用Vue的axios库来获取分类数据。您可以在uniapp的main.js文件添加以下代码: import axios from 'axios'; Vue.prototype.$http = axios; Vue.prototype.$http.get('/api/categories') .then(response => { this.categories = response.data; }) .catch(error => { console.log(error); }); 4.在侧边栏显示分类 在侧边栏显示分类。您可以使用uniapp的组件来创建侧边栏,如u-slide-menu和u-list组件。您可以在侧边栏使用v-for指令来遍历分类数据,然后将每个分类显示为列表项。如下所示: <u-slide-menu :open.sync="open"> <u-list> <u-list-item v-for="category in categories" :key="category.id"> {{ category.name }} </u-list-item> </u-list> </u-slide-menu> 5.显示分类的产品 当用户单击分类时,显示该分类的产品。您可以在分类表添加产品数据,并将产品与分类关联。然后,在单击分类时,使用axios来获取该分类的产品数据,并显示该数据。如下所示: methods: { onCategoryClick(category) { this.$http.get(`/api/categories/${category.id}/products`) .then(response => { this.products = response.data; }) .catch(error => { console.log(error); }); } } 以上是实现侧边分类导航栏的基本步骤。您可以将其扩展以满足您的需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值