vue3案例2

这是一个使用Vue.js编写的组件模板,展示了如何通过v-for指令遍历数据列表,以及如何使用事件处理方法(@click)来改变组件状态。模板中包含导航菜单和头部筛选项,根据用户选择在不同内容间切换。同时,文章也运用了reactive和ref进行状态管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<template>
  <section>
    <aside>
      <ul class="nav-ul">
        <li
          class="nav-li"
          :class="nav.active && 'grey-li'"
          v-for="nav in navArr"
          :key="nav.id"
          @click="onNav(nav)"
        >
          {{ nav.name }}
        </li>
      </ul>
    </aside>
    <main>
      <template v-if="isImgActive == 1">
        <div class="header-1">
          <span
            :class="[h1.active && 'blue-p', 'default-p']"
            v-for="h1 in imgHeader1"
            :key="h1.id"
            @click="onH1(h1)"
            >{{ h1.name }}</span
          >
        </div>
        <div class="header-2">
          <span
            :class="[h2.active && 'blue-p1', 'default-p1']"
            v-for="h2 in imgHeader2"
            :key="h2.id"
            @click="onH2(h2)"
            >{{ h2.name }}</span
          >
        </div>
        <div>content{{ isImgActive1 }} {{ isImgActive2 }}</div>
      </template>
      <template v-if="isImgActive == 2">文件</template>
      <template v-if="isImgActive == 3">链接</template>
      <template v-if="isImgActive == 4">小程序</template>
      <template v-if="isImgActive == 5">视频</template>
    </main>
  </section>
</template>


<script setup>
import { reactive, ref } from "vue";
let isImgActive = ref(1);
let isImgActive1 = ref(1);
let isImgActive2 = ref(1);
let navArr = reactive([
  { id: 1, name: "图片", active: true },
  { id: 2, name: "文件" },
  { id: 3, name: "链接" },
  { id: 4, name: "小程序" },
  { id: 5, name: "视频" },
]);
let imgHeader1 = reactive([
  { id: 1, name: "海报", active: true },
  { id: 2, name: "素材" },
]);
let imgHeader2 = reactive([
  { id: 1, name: "全部", active: true },
  { id: 2, name: "分类1" },
  { id: 3, name: "素材2" },
  { id: 4, name: "素材3" },
  { id: 5, name: "素材4" },
]);

const onNav = (nav) => {
  isImgActive.value = nav.id;
  navArr.forEach((el) => {
    if (el.id == nav.id) {
      el.active = true;
    } else {
      el.active = false;
    }
  });
  // 图片区域
  if (isImgActive.value != 1) {
    imgHeader1.forEach((el) => {
      if (el.id == 1) {
        el.active = true;
      } else {
        el.active = false;
      }
    });
  }
  if (isImgActive.value != 1) {
    imgHeader2.forEach((el) => {
      if (el.id == 1) {
        el.active = true;
      } else {
        el.active = false;
      }
    });
  }
};

const onH1 = (h1) => {
  isImgActive1.value = h1.id;
  isImgActive2.value = 1;
  imgHeader1.forEach((el) => {
    if (el.id == h1.id) {
      el.active = true;
    } else if (el.id != h1.id) {
      el.active = false;
    }
  });
  if (isImgActive2.value == 1) {
    imgHeader2.forEach((el) => {
      if (isImgActive2.value == el.id) {
        el.active = true;
      } else {
        el.active = false;
      }
    });
  }
};

const onH2 = (h2) => {
  isImgActive2.value = h2.id;
  imgHeader2.forEach((el) => {
    if (el.id == h2.id) {
      el.active = true;
    } else if (el.id != h2.id) {
      el.active = false;
    }
  });
};
</script>

<style>
section {
  display: flex;
  justify-content: space-between;
  height: 100%;
}
aside {
  width: 100px;
  border: 1px solid #f00;
}
main {
  flex: 1;
  border: 1px solid #0f0;
}
.nav-ul {
  padding: 0;
  margin: 0;
}
.nav-ul > .nav-li {
  list-style: none;
  padding: 5px 0;
  text-align: center;
}
.grey-li {
  background-color: #ddd;
  color: #fff;
}

.default-p {
  display: inline-block;
  padding: 5px 10px 5px 5px;
}
.blue-p {
  color: #4b69c6;
}

.default-p1 {
  display: inline-block;
  padding: 5px 10px;
  border-radius: 20px;
  text-align: center;
}
.blue-p1 {
  background-color: #4b69c6;
  color: #fff;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值