当前位置:首页 > VUE

vue实现卡片

2026-02-10 16:29:35VUE

Vue 实现卡片组件

在 Vue 中实现卡片组件可以通过多种方式完成,以下是几种常见的方法:

使用原生 HTML 和 CSS

创建一个基本的卡片结构,利用 Vue 的数据绑定和样式控制功能:

<template>
  <div class="card">
    <div class="card-header">
      {{ title }}
    </div>
    <div class="card-body">
      {{ content }}
    </div>
    <div class="card-footer">
      <button @click="handleClick">点击</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: String,
    content: String
  },
  methods: {
    handleClick() {
      this.$emit('card-clicked');
    }
  }
};
</script>

<style scoped>
.card {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 16px;
  margin: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.card-header {
  font-weight: bold;
  margin-bottom: 8px;
}
.card-body {
  margin-bottom: 8px;
}
.card-footer {
  text-align: right;
}
</style>

使用 UI 框架(如 Element UI、Vuetify 等)

许多流行的 Vue UI 框架提供了现成的卡片组件:

<template>
  <el-card :header="title">
    {{ content }}
    <template #footer>
      <el-button @click="handleClick">点击</el-button>
    </template>
  </el-card>
</template>

<script>
export default {
  props: {
    title: String,
    content: String
  },
  methods: {
    handleClick() {
      this.$emit('card-clicked');
    }
  }
};
</script>

动态卡片列表

展示一个卡片列表,使用 v-for 指令:

<template>
  <div class="card-list">
    <div v-for="(card, index) in cards" :key="index" class="card">
      <div class="card-header">{{ card.title }}</div>
      <div class="card-body">{{ card.content }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cards: [
        { title: '卡片1', content: '内容1' },
        { title: '卡片2', content: '内容2' }
      ]
    };
  }
};
</script>

带交互功能的卡片

实现一个可折叠的卡片:

vue实现卡片

<template>
  <div class="card">
    <div class="card-header" @click="toggle">
      {{ title }}
      <span>{{ isOpen ? '▲' : '▼' }}</span>
    </div>
    <div class="card-body" v-show="isOpen">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: String,
    content: String
  },
  data() {
    return {
      isOpen: false
    };
  },
  methods: {
    toggle() {
      this.isOpen = !this.isOpen;
    }
  }
};
</script>

这些方法涵盖了从基础到高级的卡片实现方式,可以根据项目需求选择合适的方案。

标签: 卡片vue
分享给朋友:

相关文章

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…

vue实现等级选择

vue实现等级选择

实现等级选择的方法 在Vue中实现等级选择功能,可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for渲染星级选择 通过v-for指令循环渲染星级图标,结合点击事件实现等级选择:…

vue弹幕实现暂停

vue弹幕实现暂停

实现 Vue 弹幕暂停功能 监听播放状态 通过 v-model 或自定义事件绑定弹幕组件的播放状态,存储一个布尔值(如 isPlaying)控制播放与暂停。在弹幕滚动逻辑中根据该状态判断是否更新位置。…

vue实现word在线

vue实现word在线

Vue 实现 Word 在线编辑与预览 方案一:使用 docx.js 与前端解析 安装依赖库 docx 和 file-saver,用于生成和下载 Word 文件: npm install docx…

vue缺省页实现

vue缺省页实现

Vue 缺省页实现方法 使用条件渲染控制显示 通过v-if或v-show指令控制缺省页的显示。当数据为空时展示缺省组件,否则显示正常内容。 <template> <div>…