当前位置:首页 > VUE

Vue实现遍历并交互

2026-01-21 07:34:43VUE

Vue实现遍历并交互

在Vue中,可以使用v-for指令遍历数组或对象,并结合事件绑定实现交互功能。以下是一个完整的实现方法:

Vue实现遍历并交互

遍历数组并绑定点击事件

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

<script>
export default {
  data() {
    return {
      items: [
        { name: 'Item 1', id: 1 },
        { name: 'Item 2', id: 2 },
        { name: 'Item 3', id: 3 }
      ]
    }
  },
  methods: {
    handleClick(item) {
      console.log('Clicked:', item)
    }
  }
}
</script>

遍历对象并显示/隐藏内容

<template>
  <div>
    <div 
      v-for="(value, key) in userInfo" 
      :key="key"
      @mouseover="showTooltip(key)"
      @mouseout="hideTooltip"
    >
      {{ key }}: {{ value }}
      <div v-if="activeTooltip === key" class="tooltip">
        Additional info about {{ key }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {
        name: 'John',
        age: 30,
        email: 'john@example.com'
      },
      activeTooltip: null
    }
  },
  methods: {
    showTooltip(key) {
      this.activeTooltip = key
    },
    hideTooltip() {
      this.activeTooltip = null
    }
  }
}
</script>

动态修改遍历的数据

<template>
  <div>
    <div v-for="(task, index) in tasks" :key="index">
      <input 
        type="checkbox" 
        v-model="task.completed"
        @change="updateTask(index)"
      >
      <span :class="{ 'completed': task.completed }">
        {{ task.text }}
      </span>
      <button @click="removeTask(index)">Remove</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tasks: [
        { text: 'Learn Vue', completed: false },
        { text: 'Build app', completed: false },
        { text: 'Deploy', completed: false }
      ]
    }
  },
  methods: {
    updateTask(index) {
      console.log('Task updated:', this.tasks[index])
    },
    removeTask(index) {
      this.tasks.splice(index, 1)
    }
  }
}
</script>

使用计算属性实现过滤遍历

<template>
  <div>
    <input v-model="searchQuery" placeholder="Search...">
    <ul>
      <li v-for="product in filteredProducts" :key="product.id">
        {{ product.name }} - ${{ product.price }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      products: [
        { id: 1, name: 'Laptop', price: 999 },
        { id: 2, name: 'Phone', price: 699 },
        { id: 3, name: 'Tablet', price: 399 }
      ]
    }
  },
  computed: {
    filteredProducts() {
      return this.products.filter(product => {
        return product.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      })
    }
  }
}
</script>

这些示例展示了Vue中遍历数据的多种方式以及如何实现交互功能,包括点击事件、鼠标事件、动态修改数据和过滤显示内容。

标签: 遍历Vue
分享给朋友:

相关文章

Vue 实现登出

Vue 实现登出

Vue 实现登出的方法 清除用户状态 在Vue应用中,登出通常需要清除用户的登录状态。可以通过清除Vuex中的用户数据或本地存储的token来实现。 // 在Vuex的actions中 logout…

Vue gitbook 实现

Vue gitbook 实现

Vue 与 GitBook 集成实现 将 Vue 集成到 GitBook 中可以通过插件或自定义构建流程实现,以下是具体方法: 使用 gitbook-plugin-vue 插件 安装插件到 GitB…

Vue实现路由监

Vue实现路由监

Vue 实现路由监听的方法 在 Vue 中可以通过多种方式实现路由监听,以下是常见的几种方法: 使用 watch 监听 $route 对象 通过 Vue 的 watch 功能可以监听 $route…

Vue前端路由实现

Vue前端路由实现

Vue前端路由的实现方式 Vue前端路由通常通过vue-router库实现,它是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。以下是核心实现方法和步骤: 安装vue-router 通过…

Vue 实现左右滑动

Vue 实现左右滑动

Vue 实现左右滑动的方法 使用 touch 事件监听 通过监听 touchstart、touchmove 和 touchend 事件实现基础滑动逻辑。在 Vue 组件中声明这些事件处理函数,计算滑动…

Vue实现滚动字幕

Vue实现滚动字幕

Vue实现滚动字幕的方法 使用CSS动画实现 通过CSS的@keyframes和transform属性实现水平滚动效果,结合Vue的动态绑定控制内容。 <template> <…