详细分析Vue3中的reactive(附Demo)

1. 基本知识

reactive 是一个函数,用于将一个普通的 JavaScript 对象转换为响应式对象

当对象的属性发生变化时,Vue 会自动追踪这些变化,并触发相应的更新

Vue2没有,而Vue3中有,为啥使用这个reactive呢?

  • 响应式数据绑定:Vue 3 通过 reactive 实现了更高效的响应式系统
  • 组合式 API:相比于 Vue 2 的选项式 API,组合式 API 提供了更好的逻辑复用和代码组织方式
  • 更细粒度的 reactivity:通过 reactive,可以实现更细粒度的响应式数据追踪

基本的用法如下:

import { reactive } from 'vue';

const state = reactive({
  count: 0
});

function increment() {
  state.count++;
}

2. 用法

详细用法有如下:

  1. 创建响应式对象
import { reactive } from 'vue';

const state = reactive({
  message: 'Hello Vue 3!'
});

console.log(state.message); // 输出: Hello Vue 3!
state.message = 'Hello World!';
console.log(state.message); // 输出: Hello World!
  1. 嵌套对象
const state = reactive({
  user: {
    name: 'Alice',
    age: 25
  },
  items: ['item1', 'item2']
});

state.user.age = 26; // 追踪变化
state.items.push('item3'); // 追踪变化
  1. 与 computed 结合使用
import { reactive, computed } from 'vue';

const state = reactive({
  count: 1
});

const doubleCount = computed(() => state.count * 2);

console.log(doubleCount.value); // 输出: 2
state.count++;
console.log(doubleCount.value); // 输出: 4
  1. 与 watch 结合使用
import { reactive, watch } from 'vue';

const state = reactive({
  count: 1
});

watch(() => state.count, (newValue, oldValue) => {
  console.log(`count changed from ${oldValue} to ${newValue}`);
});

state.count++; // 控制台输出: count changed from 1 to 2

3. Demo

总体Demo如下:

# 使用 Vue CLI
vue create my-vue3-app

# 使用 Vite
npm init vite@latest my-vue3-app -- --template vue
cd my-vue3-app
npm install

编写组件:

<template>
  <div>
    <p>Count: {{ state.count }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>

<script>
import { reactive } from 'vue';

export default {
  setup() {
    const state = reactive({
      count: 0
    });

    function increment() {
      state.count++;
    }

    return {
      state,
      increment
    };
  }
};
</script>

<style scoped>
button {
  margin-top: 10px;
}
</style>

使用组件:

<template>
  <div id="app">
    <Counter />
  </div>
</template>

<script>
import Counter from './components/Counter.vue';

export default {
  components: {
    Counter
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

运行项目:npm run dev

在实战中截图如下:

在这里插入图片描述

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3,`reactive`是一个函数,用于将数据对象转换为响应式对象。这意味着当数据对象发生变化时,相关的组件会自动重新渲染。 你可以使用它来实现对状态数据的响应式处理。例如,在Vue3,你可以使用`reactive`来将`state`对象转换为响应式对象,使得当`state`发生变化时,组件会响应式地更新。这可以通过以下代码来实现: ```javascript import { reactive } from "vue" const state = reactive({ count: 0, message: "Hello Vue3" }) ``` 在上面的代码,我们使用`reactive`将`state`对象转换为响应式对象。这样,当`state`的`count`或`message`属性发生变化时,相关的组件会自动重新渲染。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3-ts-demo:Vue3 + TypeScript + Vant](https://download.csdn.net/download/weixin_42099116/16349426)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [基于vue3 + ts + vite项目初探](https://blog.csdn.net/m0_49569569/article/details/124782445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值