Compare commits
2 Commits
72a4bec26a
...
9aad0f6c74
| Author | SHA1 | Date | |
|---|---|---|---|
| 9aad0f6c74 | |||
| 7fc2292d4b |
@@ -1,2 +1,3 @@
|
||||
export { reactive } from './reactive'
|
||||
export { effect } from './effect'
|
||||
export { ref } from './ref'
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { mutableHandlers } from './baseHandlers'
|
||||
import { isObject } from '@vue/shared'
|
||||
|
||||
/**
|
||||
* 响应性Map缓存对象
|
||||
@@ -39,3 +40,6 @@ function createReactiveObject(
|
||||
proxyMap.set(target, proxy)
|
||||
return proxy
|
||||
}
|
||||
|
||||
export const toReactive = <T extends unknown>(value: T): T =>
|
||||
isObject(value) ? reactive(value as object) : value
|
||||
|
||||
42
packages/reactivity/src/ref.ts
Normal file
42
packages/reactivity/src/ref.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { createDep, Dep } from './dep'
|
||||
import { toReactive } from './reactive'
|
||||
import { activeEffect, track, trackEffects } from './effect'
|
||||
|
||||
export interface Ref<T = any> {
|
||||
value: T
|
||||
}
|
||||
|
||||
export function ref(value?: unknown) {
|
||||
return createRef(value, false)
|
||||
}
|
||||
function createRef(rawValue: unknown, shallow: boolean) {
|
||||
if (isRef(rawValue)) {
|
||||
return rawValue
|
||||
}
|
||||
return new RefImpl(rawValue, shallow)
|
||||
}
|
||||
class RefImpl<T> {
|
||||
private _value: T
|
||||
public dep?: Dep = undefined
|
||||
public readonly __v_isRef = true
|
||||
constructor(
|
||||
value: T,
|
||||
public readonly __v_isShallow: boolean
|
||||
) {
|
||||
this._value = __v_isShallow ? value : toReactive(value)
|
||||
}
|
||||
get value() {
|
||||
trackRefValue(this)
|
||||
return this._value
|
||||
}
|
||||
set value(newValue) {}
|
||||
}
|
||||
export function trackRefValue(ref) {
|
||||
if (activeEffect) {
|
||||
trackEffects(ref.dep || (ref.dep = createDep()))
|
||||
}
|
||||
}
|
||||
//是否为ref
|
||||
export function isRef(r: any): r is Ref {
|
||||
return !!(r && r.__v_isRef === true)
|
||||
}
|
||||
@@ -1,2 +1,5 @@
|
||||
//判断是否为一个数组
|
||||
export const isArray = Array.isArray
|
||||
|
||||
export const isObject = (val: unknown) =>
|
||||
val !== null && typeof val === 'object'
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Document</title>
|
||||
<script src="../../dist/vue.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">
|
||||
<p id="p1"></p>
|
||||
<p id="p2"></p>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
const { reactive } = Vue
|
||||
const obj=reactive('张三')//不能接收简单数据类型
|
||||
console.log(obj)
|
||||
</script>
|
||||
</html>
|
||||
@@ -1 +1 @@
|
||||
export { reactive, effect } from '@vue/reactivity'
|
||||
export { reactive, effect, ref } from '@vue/reactivity'
|
||||
|
||||
Reference in New Issue
Block a user