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

337
Visualizações
Update props value in vue

How to update props value by method in vue? So I have props width with default value is 20

<div :style="{ width: width+ 'px'}">
props: {
  width: {
    type: [String, Number],
    default: '20px'
  },
}

and then it will rendered the component to width=20px. Can I change the props value by using javascript? Example it might look like this(?)

myEventHandler(e) {
  if(window.innerWidth < 768 && this.width === '20')
  this.width = '' // I want to revert the value to empty like it never used this props before. But I get vue warn that the prop being mutated
},

Any help would be very helful, thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As stated in the VueJs documentation: https://vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow

This means you should not attempt to mutate a prop inside a child component. If you do, Vue will warn you in the console.

If you really really need to mutate the props, you can try something like: In the child component:

props: {
  width: {
    type: [String, Number],
    default: '20px'
  }
},
computed: {
  computedWidth: {
    get(){
      return this.width
    },
    set(newValue) {
      this.$emit('widthChanged', newValue)
    }
  }
}

In the parent component:

<child-component
  :width="width"
  @widthChanged="someFunctionWhoMutateYourVar"
>

This should do the work

about 4 years ago · Juan Pablo Isaza 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