0

I am trying to change the value by clicking on the button, but Vue does not update this value, although it is updated in the console:

<script setup>
import { ref } from 'vue'
let value = ref(false);
function update() {
    value =! value
    console.log(value); // changed
}
</script>

<template>
  <button @click="update">UPDATE</button>
  <p>
    {{ value }} - not changed
  </p>

</template>

I created an example in Vue SFC Playground: https://sfc.vuejs.org/example_not_change_value

Where do I make a mistake and how to fix it?

1
  • You can always prefer const to let where applicable to avoid accidental reassignments Commented Mar 13, 2023 at 8:40

1 Answer 1

1

You should use .value in script when using Ref.
This may help.

<script setup>
import { ref } from 'vue'
let value = ref(false);
function update() {
    value.value = !value.value
    console.log(value.value); // changed
}
</script>

<template>
  <button @click="update">UPDATE</button>
  <p>
    {{ value }} - not changed
  </p>

</template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.