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

/ 67,706评论 / 401581阅读 / 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. jonbian.co说道:

    Pure Bluffing In Poker – Why It Is Really A Profitable Act 다바오
    포커 설치 방법 (jonbian.co)

  2. WilliamVet说道:

    квартира в новостройке от застройщика https://kvartiranovostroi.ru

  3. Allenplesy说道:

    купить квартиру от застройщика https://zastroyshikekb54.ru

  4. Matthewstype说道:

    квартиры с отделкой от застройщика https://novyekvartiry2.ru

  5. How To Get To Five Good Of The Marketing Food Chain 윌슨 농구공 에볼루션

  6. Jamesjer说道:

    buy doxycycline us: doxycycline 100 mg forsale outside the us – cheap doxy

  7. WilliamDop说道:

    купить квартиру новостройка застройщика отделкой https://newkvartiry-spb.ru

  8. Saved as a favorite, I really like your blog.

  9. ThomasSkawl说道:

    квартиры от застройщика цены жк https://novye-kvartiry-spb.ru

  10. Jamesjax说道:

    новые квартиры от застройщиков купить 3 комнатную квартиру

  11. Hello, Neat post. There is an issue together with your web site in internet explorer, could check this?
    IE still is the marketplace leader and a large element of other people will leave
    out your great writing due to this problem.

  12. 보증금 대출说道:

    When You Should And Will Not Get Credit 보증금 대출

  13. Jamesjer说道:

    best price for doxycycline 100 mg without prescription: doxycycline 50mg tablets price – doxycycline online pharmacy canada

  14. Jamesjer说道:

    buying clomid for sale: can i order generic clomid pill – cost of clomid without dr prescription

  15. Way cool! Some extremely valid points! I appreciate you penning this article and also the rest of the website is also really good.

  16. Jamesjer说道:

    doxycycline with out a rx: doxycycline 500 mg capsules – how much is doxycycline cost

  17. Great Solutions To Generate A Boost In Traffic To Expenses – Part 1 검색엔진최적화 배우기

  18. 비아그라(성분명: 실데나필)를 처음 복용하려는 사용자들을 위한 가이드를 제공합니다. 이 가이드는 비아그라를 안전하고 효과적으로 사용하는 데 도움이 됩니다.

    비아그라 복용 가이드

  19. 博彩论坛说道:

    After looking into a number of the blog posts on your web page, I seriously appreciate your way of writing a blog. I book marked it to my bookmark site list and will be checking back in the near future. Please visit my website as well and let me know what you think.

  20. solventworks.com说道:

    Option One Mortgage Loans – Getting An Option Arm Or Option One Mortgage Loan 전세 대출
    (solventworks.com)

  21. Trefath说道:

    Привет!
    Полезные советы по безопасной покупке диплома о высшем образовании
    mockwacom.getbb.ru/viewtopic.php?f=11&t=1728&p=2364#p2364
    Поможем вам всегда!.

  22. Emanuel Smack说道:

    There’s definately a great deal to learn about this topic. I like all of the points you made.

  23. Seo: Five Important Points You Must Know About Backlinks seo 마케팅 – http://mchughontheweb.com/

  24. Lazrimd说道:

    Здравствуйте!
    Приобрести диплом о высшем образовании.
    tosiya.ru/cb-profile/pluginclass/cbblogs.html?action=blogs&func=show&id=298

  25. Jamesjer说道:

    cipro online no prescription in the usa: cipro online no prescription in the usa – ciprofloxacin

  26. How To Further Improve Your Seo Reporting Skills 워드프레스 seo

  27. Dnrtyvb说道:

    Привет!
    Купить документ о получении высшего образования можно у нас в столице.
    ast-diplom24.ru/otzyvy
    Успехов в учебе!

  28. Roberthearp说道:

    купить однокомнатную квартиру в новостройке недорогие квартиры от застройщика

  29. GregorySal说道:

    Indibet is a premier online casino offering a wide array of games including slots, table games, and live dealer options. Renowned for its user-friendly interface and robust security measures, Indibet ensures a top-notch gaming experience with exciting bonuses and 24/7 customer support.

  30. Diplomi_fuol说道:

    Здравствуйте!
    Заказать документ университета вы можете в нашем сервисе.
    diploms-x.com/kupit-diplom-bakalavra-ili-specialista 

  31. DavidFex说道:

    The future football star Shabab Al-Ahly https://dubai.shabab-al-ahli-ar.com was born in Dubai in 2000. From a young age, he showed exceptional football abilities and joined the youth academy of one of the UAE’s leading clubs, Shabab Al-Ahly.

  32. MichaelDrare说道:

    The fascinating story of Ja Morant’s https://spain.atletico-madrid-ar.com meteoric rise, from status from rookie to leader of the Memphis Grizzlies and rising NBA superstar.

  33. GeorgeViarl说道:

    In the world of football, Atletico Madrid https://spain.atletico-madrid-ar.com has long been considered the second most important club in Spain after the dominant, Real Madrid.

  34. JamesUname说道:

    Galatasaray https://turkey.galatasaray-ar.com is one of the most famous football clubs in Turkiye, with a glorious and eventful history.

  35. Jamesjer说道:

    generic doxycycline 200 mg: doxycycline online pharmacy uk – doxycycline 150 mg cost

  36. Things Everyone Ought To Know About Getting An Advance Payday Loan 리드코프 무직자 대출

发表回复

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