Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!