Vue.js 代码:实现一个多标签卡片页面

应用场景介绍

该 Vue.js 代码用于创建一个具有多个标签页签的多标签卡片页面。此类页面通常用于组织和显示不同类型的信息或内容,例如产品详细信息、用户资料或统计数据。

代码基本功能介绍

此代码的主要功能如下:

  • **多标签页签切换:**用户可以点击顶部标签页签在不同内容面板之间切换。
  • **内容面板切换:**当用户切换标签页签时,相应的卡片内容面板会显示。
  • **动态内容加载:**根据选定的标签页签,动态加载不同的卡片内容。

功能实现步骤及关键代码分析

1. 组件模板

<template>
  <div class="page">
    <div class="header">
      <div class="back-icon" @click="goBack">
        <svg>...</svg>
      </div>
      <div class="title">The Striped Hyena</div>
      <div class="more-icon">
        <svg>...</svg>
      </div>
    </div>
    <div class="content">
      <div class="banner">
        <img :src="`https://source.unsplash.com/random/375x200`" alt="The Striped Hyena" />
      </div>
      <div class="tabs">
        <div class="tab" :class="{ active: tab === 'general' }" @click="tab = 'general'">
          General
        </div>
        <div class="tab" :class="{ active: tab === 'gallery' }" @click="tab = 'gallery'">
          Gallery
        </div>
        <div class="tab" :class="{ active: tab === 'reports' }" @click="tab = 'reports'">
          Reports
        </div>
      </div>
      <div class="tab-content">
        <div v-if="tab === 'general'" class="general">...</div>
        <div v-if="tab === 'gallery'" class="gallery">...</div>
        <div v-if="tab === 'reports'" class="reports">...</div>
      </div>
    </div>
    <div class="footer">...</div>
  </div>
</template>
  • 组件模板定义了页面的结构,包括头部、内容区和底部导航栏。
  • tabs 部分包含三个标签页签,当用户点击时会触发 tab 变量的变化,从而切换到相应的卡片内容面板。

2. 组件脚本

<script lang="tsx" setup>
import { ref, onMounted } from 'vue'

const tab = ref('general')
const images = [
  'https://source.unsplash.com/random/375x200',
  'https://source.unsplash.com/random/375x200',
  'https://source.unsplash.com/random/375x200',
  'https://source.unsplash.com/random/375x200',
  'https://source.unsplash.com/random/375x200',
  'https://source.unsplash.com/random/375x200',
]

onMounted(() => {
  // Do something after the page is mounted
})

const goBack = () => {
  // Go back to the previous page
}
</script>
  • setup 函数使用 Vue.js 的 refonMounted 钩子来管理组件状态和生命周期。
  • tab 变量用于跟踪当前选定的标签页签。
  • images 数组包含了用于填充画廊卡片的图像 URL。
  • onMounted 钩子在组件挂载后执行,可以在这里执行初始化操作。

3. 样式

.page {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  background-color: #fff;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 56px;
  padding: 0 16px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.content {
  margin-top: 56px;
  padding: 16px;
}

.tabs {
  display: flex;
  align-items: center;
  justify-content: space-around;
  margin-top: 16px;
}

.tab {
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.tab.active {
  background-color: #007bff;
  color: #fff;
}

.tab-content {
  margin-top: 16px;
}
  • 样式定义了页面的整体外观和布局,包括头部、内容区、标签页签和卡片内容面板。
  • 当标签页签处于激活状态时,它将应用 active 样式,并更改背景颜色和文本颜色。

总结与展望

此 Vue.js 代码提供了构建多标签卡片页面的基础。该代码可以根据需要进行扩展和定制,以满足特定的应用程序要求。

开发经验与收获:

  • 了解了 Vue.js 中使用 refonMounted 钩子来管理组件状态和生命周期的用法。
  • 掌握了在 Vue.js 中使用 v-if 指令动态显示内容。
  • 熟悉了 Vue.js 中使用 CSS 类来控制组件外观。

未来该卡片功能的拓展与优化:

  • **内容异步加载:**优化卡片内容的加载方式,以提高性能和响应速度。
  • **卡片拖拽排序:**允许用户拖拽卡片以重新排列顺序。
  • **卡片内容编辑:**集成编辑器组件,允许用户直接在卡片中编辑内容。
Login
ECHO recommendation
ScriptEcho.ai

User Annotations

斑鬣狗图鉴