PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 16, 2022

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

 October 16, 2022     vue.js, vuejs3     No comments   

Issue

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.

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)


Answered By - sungryeol
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing