Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
How to create and use computed property's setter with the composition API

I have a Vue 2 app and it's using the composition API package so that I can create vue components with composition API.

How does one create and use a setter for a computed property with the Vue composition API?

I am looking at the docs, and I do not see any documentation for creating a setter for a computed property. In the test suite, I see an example:

 const b = computed({
   get: () => a.value + 1,
   set: (v) => (a.value = v - 1),
 })

However I am not too sure how to access the set method on b. Does anyone know how to create a computed property with a set method for the composition API? And how can I use the set method so I can change the computed property's value?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In a vue3 component, you would use it like this:

<template>
  <p>a={{ a }} b={{ b }}</p>
  <button @click="b = 3">Click me</button>
</template>

<script setup>
import {computed, ref} from 'vue';

const a = ref(1);
const b = computed({
  get: () => a.value + 1,
  set: (v) => (a.value = v - 1),
})
</script>

The setter for b is called when clicking the button (assignment b = 3)

An alternative is to use computed section inside the <script> tag (without the <script setup> section):

<template>
  <p>a={{a}} b={{b}}</p>
  <button @click="b = 3">Click me 1</button>
</template>


<script>
export default {
  name: "Test",
  data(){
    return {
      a: 1
    }
  },
  computed: {
    b:{
      get(){return this.a + 1;},
      set(v){this.a = v - 1;}
    }
  }
}
</script>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda