当前位置:首页 > VUE

vue实现列表demo

2026-01-18 09:51:33VUE

以下是一个基于 Vue.js 的列表实现示例,包含基础功能与关键代码解析:

基础列表渲染

<template>
  <div>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { name: 'Item 1' },
        { name: 'Item 2' },
        { name: 'Item 3' }
      ]
    }
  }
}
</script>

带交互功能的列表

<template>
  <div>
    <button @click="addItem">Add Item</button>
    <ul>
      <li v-for="(item, index) in items" :key="item.id">
        {{ item.text }}
        <button @click="removeItem(index)">×</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Sample Item 1' },
        { id: 2, text: 'Sample Item 2' }
      ],
      nextId: 3
    }
  },
  methods: {
    addItem() {
      this.items.push({
        id: this.nextId,
        text: `New Item ${this.nextId}`
      })
      this.nextId++
    },
    removeItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

动态样式绑定

<template>
  <ul>
    <li 
      v-for="item in filteredItems" 
      :key="item.id"
      :class="{ 'active': item.isActive }"
      @click="toggleActive(item)"
    >
      {{ item.name }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Apple', isActive: false },
        { id: 2, name: 'Banana', isActive: true }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => item.name.includes('a'))
    }
  },
  methods: {
    toggleActive(item) {
      item.isActive = !item.isActive
    }
  }
}
</script>

<style>
.active {
  background-color: #f0f0f0;
  font-weight: bold;
}
</style>

服务端数据获取

<template>
  <div v-if="loading">Loading...</div>
  <ul v-else>
    <li v-for="post in posts" :key="post.id">
      <h3>{{ post.title }}</h3>
      <p>{{ post.body }}</p>
    </li>
  </ul>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      posts: [],
      loading: true
    }
  },
  async created() {
    try {
      const response = await axios.get('https://jsonplaceholder.typicode.com/posts')
      this.posts = response.data.slice(0, 5)
    } catch (error) {
      console.error(error)
    } finally {
      this.loading = false
    }
  }
}
</script>

关键注意事项:

vue实现列表demo

  • 始终为 v-for 指定唯一的 :key 属性
  • 复杂列表建议使用计算属性进行数据处理
  • 大量数据考虑使用虚拟滚动优化性能
  • 表单操作时注意 Vue 的响应式更新机制

标签: 列表vue
分享给朋友:

相关文章

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本流程 在Vue单页面应用中实现登录功能,通常需要以下几个关键步骤: 创建登录表单组件 使用Vue的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 <…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue实现兼容

vue实现兼容

Vue 兼容性实现方案 Vue.js 的兼容性主要涉及对不同浏览器、Vue 版本以及第三方库的支持。以下是常见的兼容性解决方案: 浏览器兼容性 使用 Babel 转换 ES6+ 代码为 ES5 语…

vue实现tabbar

vue实现tabbar

Vue 实现 TabBar 的方法 使用 Vue Router 实现基础 TabBar 安装 Vue Router 依赖: npm install vue-router 创建路由配置文件(route…