阅读视图

发现新文章,点击刷新页面。

Vue3响应式API-reactive的原理

这段代码实现了一个简化版的响应式系统(类似 Vue 3 的 reactive),包含依赖收集和触发更新的核心机制。让我详细讲解每个部分:

一、核心结构

1. reactive 函数

function reactive<T extends object>(target: T) {
  return createReactiveObject(target);
}
  • 入口函数,接收一个普通对象
  • 返回该对象的响应式代理
  • 使用泛型 T extends object 确保只能处理对象类型

2. createReactiveObject 函数

创建 Proxy 代理对象的核心函数:

function createReactiveObject<T extends object>(target: T) {
  const handler = {
    // get 拦截器
    get(target: object, key: keyof object, receiver: () => void) {
      const result = Reflect.get(target, key, receiver);
      track(target, key);  // 依赖收集
      if (typeof result === "object" && result !== null) {
        return createReactiveObject(result);  // 深度响应式
      }
      return result;
    },
    
    // set 拦截器
    set(target: object, key: keyof object, value: unknown, receiver: () => void) {
      const oldValue = target[key];
      const result = Reflect.set(target, key, value, receiver);
      if (oldValue !== value) {
        trigger(target, key);  // 触发更新
      }
      return result;
    },
  };
  return new Proxy(target, handler);
}

关键点:

  • 深度响应式:当访问的属性值是对象时,递归创建响应式代理
  • 惰性转换:只有在访问嵌套对象时才进行响应式转换
  • Reflect API:使用 Reflect.get/set 确保正确的 this 绑定

二、依赖管理系统

1. 存储结构

const targetMap = new WeakMap<object, Map<string, Set<Function>>>();
let activeEffect: null | Function = null;

结构说明:

WeakMap
  key: 原始对象 (target)
  value: Map
    key: 属性名 (string)
    value: Set<Function>  // 依赖该属性的 effect 集合

为什么用 WeakMap?

  • 键是对象,不影响垃圾回收
  • 当原始对象不再使用时,对应的依赖关系会自动清除

2. track - 依赖收集

function track(target: object, key: string) {
  if (!activeEffect) return;  // 没有 activeEffect 时不收集
  
  // 获取或创建 target 对应的 depsMap
  let depsMap = targetMap.get(target);
  if (!depsMap) {
    targetMap.set(target, (depsMap = new Map()));
  }
  
  // 获取或创建 key 对应的 dep(effect 集合)
  let dep = depsMap.get(key);
  if (!dep) {
    depsMap.set(key, (dep = new Set()));
  }
  
  dep.add(activeEffect);  // 将当前 effect 加入依赖集合
}

3. trigger - 触发更新

function trigger(target: object, key: keyof object) {
  const depsMap = targetMap.get(target);
  if (!depsMap) return;
  
  const dep = depsMap.get(key);
  if (!dep) return;
  
  // 创建副本避免无限循环
  const effects = new Set(dep);
  effects.forEach((effect) => {
    if (effect !== activeEffect) {  // 避免当前 effect 触发自身
      effect();
    }
  });
}

三、Effect 系统

export function effect(fn: Function) {
  const effectFn = () => {
    const prevEffect = activeEffect;
    activeEffect = effectFn;  // 设置当前活跃的 effect

    try {
      return fn();
    } finally {
      activeEffect = prevEffect;  // 恢复之前的 effect
    }
  };
  
  effectFn();  // 立即执行一次,进行初始依赖收集
  return effectFn;
}

执行流程:

  1. 创建 effectFn 包装函数
  2. 执行时设置 activeEffect = effectFn
  3. 执行用户传入的 fn()
  4. 在 fn() 执行期间,所有对响应式属性的访问都会调用 track
  5. track 将当前 activeEffect 收集为依赖
  6. 执行完成后恢复之前的 activeEffect

四、使用示例

const state = reactive<{ todos: string[] }>({ todos: [] });

// 创建一个 effect
effect(() => {
  console.log('todos 改变了:', state.todos);
  // 首次执行时,访问 state.todos,触发 get
  // track 收集当前 effect 作为 todos 的依赖
});

// 修改状态
state.todos.push('学习响应式原理');  // 触发 set -> trigger -> 执行 effect

五、完整工作流程

  1. 初始化响应式对象

    const state = reactive({ todos: [] });
    // 创建 Proxy 代理
    
  2. 创建 effect

    effect(() => console.log(state.todos));
    // 1. 设置 activeEffect = 当前 effect
    // 2. 执行回调,访问 state.todos
    // 3. Proxy.get 触发,track 收集依赖
    
  3. 数据变更

    state.todos = ['新任务'];
    // 1. Proxy.set 触发
    // 2. trigger 查找依赖的 effects
    // 3. 执行所有依赖的 effect 函数
    

Vue真的是单向数据流?

Vue 单向数据流的深入解析:对象 Props 的陷阱

🔍 核心问题:对象 Props 的可变性

你指出的完全正确!这是 Vue 单向数据流中最大的陷阱

🚨 问题的本质

1. 对象/数组 Props 的引用传递

<!-- 父组件 -->
<script setup>
import { reactive } from 'vue'
import ChildComponent from './ChildComponent.vue'

// 父组件的响应式对象
const user = reactive({
  name: '张三',
  age: 25,
  address: {
    city: '北京',
    street: '长安街'
  }
})
</script>

<template>
  <!-- 传递对象 prop -->
  <ChildComponent :user="user" />
</template>

<!-- 子组件 ChildComponent.vue -->
<script setup>
const props = defineProps(['user'])

// ⚠️ 危险操作:可以直接修改父组件的数据!
const updateUser = () => {
  props.user.name = '李四'  // ✅ 生效,且修改了父组件
  props.user.age++         // ✅ 生效
  props.user.address.city = '上海'  // ✅ 生效
  props.user.address.street = '南京路'  // ✅ 生效
}

// 甚至可以直接添加新属性
const addProperty = () => {
  props.user.email = 'new@email.com'  // ✅ 会添加到父组件对象
}
</script>

📊 为什么会出现这个问题?

2. JavaScript 引用机制 + Vue 响应式系统

// 1. JavaScript 对象是引用传递
const parentObj = { name: 'Parent' }
const childProp = parentObj  // 同一个引用!

childProp.name = 'Modified'  // 修改了 parentObj
console.log(parentObj.name)  // 'Modified'

// 2. Vue 的响应式系统基于 Proxy
const reactiveParent = reactive({ data: 'original' })
// 子组件获得的是同一个 Proxy 对象

// 3. Vue 的开发模式警告有限制
// 直接赋值 props.user = {} ❌ 会警告
// 但修改属性 props.user.name = 'new' ⚠️ 不会警告!

🔄 Vue 官方态度

3. 文档中的说明

Vue 官方文档明确指出:

"注意在 JavaScript 中对象和数组是通过引用传入的,
所以对于一个数组或对象类型的 prop 来说,
在子组件中改变变更这个对象或数组本身将会影响到父组件的状态。"

"这在 Vue 中是不被推荐的,因为它破坏了单向数据流的清晰性。"

4. 警告机制的局限

// Vue 只能检测到这些情况:

// 情况1:直接重新赋值 ❌ 会警告
props.user = { name: 'new' }
// 警告: Attempting to mutate prop "user"

// 情况2:修改基本类型 prop ❌ 会警告
props.count = 2
// 警告: Attempting to mutate prop "count"

// 情况3:修改对象/数组的属性 ⚠️ 不会警告!
props.user.name = 'new'
props.list.push('item')
// 无警告,但破坏了单向数据流

🛡️ 如何避免这个问题?

5. 解决方案1:传递解构后的值

<!-- 父组件 -->
<template>
  <!-- 传递基本类型或深度解构 -->
  <ChildComponent 
    :user-name="user.name"
    :user-age="user.age"
    :user-city="user.address.city"
  />
  
  <!-- 或者使用 computed 创建只读副本 -->
  <ChildComponent :user="readonlyUser" />
</template>

<script setup>
import { computed } from 'vue'

const user = reactive({ name: '张三', age: 25 })

// 创建只读版本
const readonlyUser = computed(() => ({
  name: user.name,
  age: user.age
}))
</script>

6. 解决方案2:使用深度只读

<!-- 父组件 -->
<script setup>
import { readonly } from 'vue'

const user = reactive({ name: '张三', age: 25 })

// 使用 readonly 包装
const readonlyUser = readonly(user)
</script>

<template>
  <ChildComponent :user="readonlyUser" />
</template>

<!-- 子组件 -->
<script setup>
const props = defineProps(['user'])

const updateUser = () => {
  // ❌ 现在会触发警告
  props.user.name = '李四'
  // 警告: Set operation on key "name" failed: target is readonly.
}
</script>

7. 解决方案3:使用深度复制

<!-- 子组件处理 -->
<script setup>
import { ref, watch } from 'vue'

const props = defineProps(['user'])

// 深度复制到本地状态
const localUser = ref(JSON.parse(JSON.stringify(props.user)))

// 或者使用 lodash 的 cloneDeep
import { cloneDeep } from 'lodash-es'
const localUser = ref(cloneDeep(props.user))

// 监听 props 变化同步更新
watch(() => props.user, (newUser) => {
  localUser.value = cloneDeep(newUser)
}, { deep: true })
</script>

📈 最佳实践模式

8. 工厂函数模式

// composables/useSafeProps.js
import { ref, watch, toRaw } from 'vue'

export function useSafeProp(propValue, options = {}) {
  const { deep = true, immediate = true } = options
  
  // 创建本地副本
  const localValue = ref(structuredClone(toRaw(propValue)))
  
  // 监听 props 变化
  watch(() => propValue, (newValue) => {
    localValue.value = structuredClone(toRaw(newValue))
  }, { deep, immediate })
  
  return localValue
}

// 在组件中使用
const props = defineProps(['user', 'list'])
const localUser = useSafeProp(props.user)
const localList = useSafeProp(props.list)

9. 类型安全的深度只读

// types/utilities.ts
export type DeepReadonly<T> = {
  readonly [P in keyof T]: T[P] extends object 
    ? T[P] extends Function 
      ? T[P] 
      : DeepReadonly<T[P]>
    : T[P]
}

// 父组件使用
import type { DeepReadonly } from '@/types/utilities'

const user = reactive({ 
  name: '张三', 
  profile: { age: 25, address: { city: '北京' } } 
})

const readonlyUser = user as DeepReadonly<typeof user>

// 传递给子组件
<ChildComponent :user="readonlyUser" />

// 子组件中 TypeScript 会阻止修改
props.user.name = 'new'  // ❌ TypeScript 错误
props.user.profile.age = 30  // ❌ TypeScript 错误

🎯 结论:Vue 真的是单向数据流吗?

正确答案

  1. 设计理念上:Vue 设计为单向数据流 ✅
  2. 语法层面:通过 props + events 强制执行单向流 ✅
  3. 实现层面:由于 JavaScript 限制,存在对象引用漏洞 ⚠️
  4. 实践层面:需要开发者自觉遵守规范 ⚠️

关键认知

  • Vue 的单向数据流是"约定大于强制"
  • 框架提供了基础,但开发者需要负责具体实现
  • 对象/数组 props 的易变性是已知的设计取舍
  • 通过工具、规范和最佳实践可以避免问题

最终建议

如果你想保持严格单向数据流:

  1. 始终使用 readonly() 包装对象 props
  2. 使用 TypeScript 的只读类型
  3. 配置 ESLint 严格规则
  4. 通过事件通信,而不是直接修改
  5. 对于复杂对象,传递解构后的基本类型 但也要理解 Vue 的设计哲学:"给开发者选择权,而不是强制约束"

Vue 的设计哲学是"渐进式"和"灵活" ,它提供了单向数据流的基础,但也允许在需要时绕过限制。这既是优点(灵活性),也是挑战(需要团队规范)。

❌