Home  >  Q&A  >  body text

Vue 3: How to correctly update component props values ​​using composition API?

I like this component:

<template>
  <div>
    <p>Current coords: <strong>{{ coords }}</strong></p>
    <button type="button" @click="updateCoords">
  </div>
</template>
<script>
export default {
  props: {
    coords: {
      type: Array,
      required: true
    }
  },
  setup(props) {
    const updateCoords = () => {
      props.coords = [38.561785, -121.449756]
      // props.coords.value = [38.561785, -121.449756]
    }
    return { updateCoords }
  },
}
</script>

I tried to update the prop coords value using the updateCoords method but got the error:

Uncaught TypeError: Cannot set property of undefined (set 'coordinate')

How to update props value correctly in my case?

P粉951914381P粉951914381207 days ago445

reply all(1)I'll reply

  • P粉306523969

    P粉3065239692024-03-26 17:28:10

    Props are read-only:
    https://v3.vuejs.org/guide/component -props.html#one-way-data-flow

    If you want to bind props in two ways, you need to implement the v-model mode:
    https://v3-migration.vuejs.org /break-changes/v-model.html#_3-x-syntax

    
    sssccc

    reply
    0
  • Cancelreply