Vue 3: How to correctly update component props values ​​using composition API?
P粉951914381
P粉951914381 2024-03-25 18:19:32
0
1
485

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粉951914381
P粉951914381

reply all(1)
P粉306523969

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template