自学内容网 自学内容网

Vue3 之ref与reactive的区别

在Vue 3中,reactive和ref都用于创建响应式的数据,但它们有一些关键的区别:

reactive用于创建响应式的对象,该对象的属性是深度响应式的。

ref用于创建响应式的基本类型数据,比如字符串、数字、布尔值等,它是reactive的简化版本,只提供了基本的响应式能力。

一、ref与reactive的区别

<template>
    <view class="">
        {{params}}  {{title}} {{arr}}
    </view>
</template>

<script setup>
    import {
        reactive,
        ref
    } from 'vue';
    
    //reactive 可用于复杂的参数
    const params = reactive({
        page: 1,
        keyword: '',
        isEnd: false
    });
    
    const arr = reactive([1, 2, 3]);
    
    const title = ref("ref与reactive的区别");
    const bool = ref(false);
    
    params.isEnd = true;
    
</script>

<style>
</style>

原文地址:https://blog.csdn.net/qq_27781261/article/details/142428140

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!