The vue child component modifies the value of the parent component

there are child components in parentheses. I click the button to pass true to the parent component to change its state, but the error says: avoid changing a PROP, directly because the value is overwritten every time the parent component is re-rendered.



export default{
  props: {
      onlyContent: {
        type: Boolean,
        default: false
      }
  },
  methods: {
      toggleContent (event) {
        if (!event._constructed) {
            return
        }
        this.onlyContent = !this.onlyContent
        this.$emit("contentToggle", this.onlyContent)
      }
  }
}


<v-ratingSelect v-on:contentToggle="contToggle" :only-content="onlyContent"></v-ratingSelect>

data () {
  return {
    onlyContent: false
  }
}

contToggle (event) {
  this.onlyContent = event
}
Mar.28,2021

modify onlyContent in the parent component

// 
toggleContent (event) {
    ...
    this.$emit('contentToggle')
}

// 
contToggle () {
  this.onlyContent = !this.onlyContent
}

this.onlyContent =! this.onlyContent , error, propvalue cannot be modified directly in subcomponents;
if you want to bind a prop bidirectionally, you can use the .sync modifier; just read the document; ide/components-custom-events.html-sharpsync-%E4%BF%AE%E9%A5%B0%E7%AC%A6" rel=" nofollow noreferrer "> sync

Menu