当前位置:首页 > VUE

Vue实现遍历并交互

2026-01-21 07:34:43VUE

Vue实现遍历并交互

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

遍历数组并绑定点击事件

<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实现的基本步骤 安装Vue.js 通过npm或CDN引入Vue.js。npm安装命令:npm install vue。CDN引入方式:<script src="https://cdn.…

Vue查看全文实现

Vue查看全文实现

Vue 查看全文实现方法 使用 v-show 或 v-if 控制显示 通过 v-show 或 v-if 指令控制文本的显示与隐藏。v-show 通过 CSS 的 display 属性切换,适合频繁切换…

Vue实现多站点

Vue实现多站点

Vue实现多站点的方案 Vue.js可以通过多种方式实现多站点架构,具体方案取决于项目需求和规模。以下是常见的几种实现方法: 基于动态路由的SPA方案 适用于中小型多站点项目,所有站点共享同一个…

Vue实现word导入

Vue实现word导入

Vue 中实现 Word 文件导入的方法 使用文件上传组件 在 Vue 中可以通过 <input type="file"> 或第三方组件(如 Element UI 的 Upload 组件)…

Vue实现postMessage

Vue实现postMessage

Vue 中使用 postMessage 进行跨窗口通信 在 Vue 中使用 postMessage 可以实现跨窗口通信,适用于不同窗口或 iframe 之间的数据传递。以下是实现方法: 发送消息…

Vue函数实现

Vue函数实现

在Vue中实现函数可以通过多种方式,具体取决于使用场景(选项式API、组合式API或生命周期钩子)。以下是常见方法: 方法1:选项式API中的methods 在Vue 2或选项式API中,函数通常…