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

/ 54,903评论 / 306908阅读 / 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. Edwardintig说道:

    https://indiamedicine.world/# mail order pharmacy india

  2. NEVADA FAKE ID说道:

    Pretty section of content. I just stumbled upon your website and in accession capital to
    assert that I acquire actually enjoyed account your blog posts.

    Any way I’ll be subscribing to your feeds and even I achievement
    you access consistently fast.

  3. AlbertFlouh说道:

    mexican pharmaceuticals online: medication from mexico pharmacy – buying prescription drugs in mexico

  4. What’s up Dear, are you genuinely visiting this site regularly, if
    so then you will absolutely obtain good know-how.

  5. Source URL说道:

    Hi! I know this is kinda off topic however , I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog article or vice-versa?

    My website goes over a lot of the same topics as yours and I think we could greatly benefit from each other.
    If you happen to be interested feel free to send me
    an email. I look forward to hearing from you! Excellent
    blog by the way!

  6. Very nice post. I simply stumbled upon your blog and wished to say that
    I’ve really enjoyed browsing your weblog posts. After all
    I’ll be subscribing in your rss feed and I hope you
    write once more soon!

  7. Robertorind说道:

    http://indiamedicine.world/# best online pharmacy india

  8. Williamwhomb说道:

    п»їbest mexican online pharmacies mexican rx online best online pharmacies in mexico

  9. AlbertFlouh说道:

    top 10 pharmacies in india: cheapest online pharmacy india – online pharmacy india

  10. yyy-casinos.net说道:

    Superb, what a weblog it is! This website provides useful facts to us, keep it up.

  11. Williamwhomb说道:

    buy prescription drugs from india online pharmacy india indianpharmacy com

发表回复

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