简体   繁体   English

vuejs 3 组合 API

[英]vuejs 3 composition API

Anyone know why the variable number doesn't display the latest value when click on it?任何人都知道为什么单击它时变量号不显示最新值? I tried using onMounted, but didn't seem to do anything.我尝试使用 onMounted,但似乎没有做任何事情。 When I clicked on the paragraph it always displays 1. I expect it to display the latest number added.当我单击该段落时,它总是显示 1。我希望它显示最新添加的数字。 Any ideas?有任何想法吗?


<script setup>

  import TheWelcome from '@/components/TheWelcome.vue'
  import { onMounted, ref  } from 'vue'
  import axios from "axios"

  let number = 1; 
    
  function getCurrentNumber() {
    
    number++;

    console.log(number)
  }

</script>


<template>

  <main>
    <TheWelcome />
  </main>
  <p @click="getCurrentNumber">Hello, this is a test to display {{number}}</p>

</template>


This is how it should be这是应该的

<script setup>
import { ref } from 'vue'

const number = ref(1); 

function getCurrentNumber() {
  number.value++
  console.log(number.value)
}
</script>


<template>
  <p @click="getCurrentNumber">Hello, this is a test to display {{ number }}</p>
</template>

Here is a working example .这是一个工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM