[Vue3]在setup中声明响应式数据(ref,reactive)

/ 54,619评论 / 305523阅读 / 2点赞

响应式数据是vue的一大亮点,下面我们来聊聊如何声明响应式数据

直接声明变量

<script setup>
let num = 0;
const clickFun = () => {
    console.log("num:", ++num);
}
</script>

<template>
    <div style="display:flex;flex-direction: column;">
        <span>num : {{num}}</span>
        <button @click="clickFun()">修改</button>
    </div>
</template>

ref(基本数据类型)

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

let num = 0;
let refNum = ref(0);
console.log("refNum:", refNum);
console.log("refNum.value", refNum.value);

const clickFun = () => {
    console.log("num:", ++num);
    console.log("refNum.value:" , ++refNum.value);
}
</script>

<template>
    <div style="display:flex;flex-direction: column;">
        <span>num : {{num}}</span>
        <span>refNum : {{refNum}}</span>
        <button @click="clickFun()">修改</button>
    </div>
</template>

reactive(对象)

<script setup>
import { ref,reactive } from 'vue';

let num = 0;
let refNum = ref(0);
let rea = reactive({
    num:0
});

console.log("refNum:", refNum);
console.log("refNum.value", refNum.value);
console.log("rea", rea);

const clickFun = () => {
    console.log("num:", ++num);
    console.log("refNum.value:" , ++refNum.value);
    console.log("rea.num:", ++rea.num);
}

</script>

<template>
    <div style="display:flex;flex-direction: column;">
        <span>num : {{num}}</span>
        <span>refNum : {{refNum}}</span>
        <span>rea.num : {{rea.num}}</span>
        <button @click="clickFun()">修改</button>
    </div>
</template>
  1. Jamesprala说道:

    amoxicillin 500mg no prescription can i buy amoxicillin over the counter – amoxicillin brand name

  2. Jamiecot说道:

    amoxicillin 500mg pill generic amoxicillin – amoxicillin 250 mg capsule

  3. Jamiecot说道:

    doxycycline hyclate 100 mg: doxycycline 100mg buy online – price doxycycline 100mg without prescription

  4. Jamesprala说道:

    doxycycline 100: doxycycline pills for sale – can you buy doxycycline over the counter in india

  5. Jamesprala说道:

    prednisone ordering online: prednisone 5 mg cheapest – prednisone 50 mg price

  6. Jamiecot说道:

    prednisone no rx: prednisone 20mg prices – prednisone without a prescription

  7. Jamiecot说道:

    doxycycline 100mg capsule sale: doxycycline canada – doxycycline for sale

  8. Jamesprala说道:

    doxycycline gel in india: 10 doxycycline gel – can you buy doxycycline

  9. Jamesprala说道:

    rexall pharmacy amoxicillin 500mg rexall pharmacy amoxicillin 500mg – can you buy amoxicillin over the counter in canada

  10. Jamiecot说道:

    cost of generic doxycycline: doxycycline 40 mg coupon – generic doxycycline 3626

  11. Jamiecot说道:

    where can i get amoxicillin 500 mg amoxicillin over the counter in canada – amoxicillin 875 125 mg tab

  12. Jamesprala说道:

    amoxicillin 500 mg brand name amoxicillin 875 125 mg tab – medicine amoxicillin 500

  13. Jamesprala说道:

    doxycycline 300 mg daily: doxycycline canadian online pharmacy – order doxycycline online uk

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注