[Vuejs]-How to refer to a private variable declared in <script> in <script setup>

3👍

  1. Don’t use this in <script setup>

  2. Options API data can be accessed through getCurrentInstance but it’s not recommended (thanks @EstusFlask)

  3. Don’t mix Options API with Composition API, if it not really necessary.

Just choose one Vue API and do all the code using it.

For Options API add

computed: 
    calculated() {
      return this.name_.length > 0 ? 'Yes' : 'No'
    }

For Composition API add

const name_ = ref("ertz")
const address_ = ref("aasf")
const phone_ = ref("100 xx 50")

const calculated = computed(() => {
  return name_.length > 0 ? 'Yes' : 'No'
})

Leave a comment