Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to declare local property from composition API in Vue 3?

In Vue 2 I would do this:

<script>
    export default {
      props: ['initialCounter'],
      data() {
        return { counter: this.initialCounter }
      }
    }
</script>

In Vue 3 I tried this:

<script setup>
   import { ref } from 'vue';
   defineProps({ 'initialCounter': Number })
   const counter = ref(props.initialCounter)
</script>

This obviously doesn’t work because props is undefined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I bind one-way properties to a local variable in Vue 3?

>Solution :

It seems the result of defineProps is not assigned as a variable. check Vue3 official doc on defineProps. Not really sure what is the use case of ref() here but toRef API can be used as well.

import { ref } from 'vue';
const props = defineProps({ 'initialCounter': Number })
const counter = ref(props.initialCounter)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading