当前位置:首页 > VUE

vue组件实现computed

2026-02-19 01:56:29VUE

Vue 组件中实现 computed 的方法

在 Vue 组件中,computed 是一种基于响应式依赖进行缓存的属性。它适用于需要根据其他数据动态计算的场景,且仅在依赖发生变化时重新计算。

基本用法

在 Vue 选项式 API 中,computed 是一个对象,其属性是计算属性的名称,值为一个函数或包含 getset 的对象。

export default {
  data() {
    return {
      firstName: 'John',
      lastName: 'Doe'
    }
  },
  computed: {
    fullName() {
      return `${this.firstName} ${this.lastName}`
    }
  }
}

带有 getter 和 setter 的计算属性

如果需要设置计算属性,可以使用包含 getset 的对象。

export default {
  data() {
    return {
      firstName: 'John',
      lastName: 'Doe'
    }
  },
  computed: {
    fullName: {
      get() {
        return `${this.firstName} ${this.lastName}`
      },
      set(newValue) {
        const [first, last] = newValue.split(' ')
        this.firstName = first
        this.lastName = last
      }
    }
  }
}

在组合式 API 中使用 computed

在 Vue 3 的组合式 API 中,computed 是一个函数,可以通过 import { computed } from 'vue' 引入。

import { ref, computed } from 'vue'

export default {
  setup() {
    const firstName = ref('John')
    const lastName = ref('Doe')

    const fullName = computed(() => {
      return `${firstName.value} ${lastName.value}`
    })

    const editableFullName = computed({
      get: () => `${firstName.value} ${lastName.value}`,
      set: (newValue) => {
        const [first, last] = newValue.split(' ')
        firstName.value = first
        lastName.value = last
      }
    })

    return { firstName, lastName, fullName, editableFullName }
  }
}

计算属性的缓存特性

计算属性会基于其依赖的响应式数据缓存结果。只有当依赖的数据发生变化时,计算属性才会重新计算。这种特性可以避免不必要的计算开销。

export default {
  data() {
    return {
      items: [1, 2, 3, 4, 5]
    }
  },
  computed: {
    filteredItems() {
      console.log('Computing filtered items')
      return this.items.filter(item => item > 2)
    }
  }
}

计算属性 vs 方法

计算属性适合用于需要缓存的复杂计算,而方法适合用于不需要缓存或需要传递参数的场景。

export default {
  data() {
    return {
      items: [1, 2, 3, 4, 5]
    }
  },
  computed: {
    computedFilter() {
      return this.items.filter(item => item > 2)
    }
  },
  methods: {
    methodFilter() {
      return this.items.filter(item => item > 2)
    }
  }
}

计算属性的依赖跟踪

计算属性会自动跟踪其依赖的响应式数据。如果依赖的数据未发生变化,计算属性会直接返回缓存的结果。

export default {
  data() {
    return {
      a: 1,
      b: 2
    }
  },
  computed: {
    sum() {
      return this.a + this.b
    }
  }
}

vue组件实现computed

标签: 组件vue
分享给朋友:

相关文章

vue实现放大效果

vue实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…