Skip to content

Commit 3d3ae3e

Browse files
committed
supported nested scope refs
1 parent db36966 commit 3d3ae3e

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/directives/scope.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
import { reactive } from '@vue/reactivity'
2-
import { isObject } from '@vue/shared'
32
import { Context } from '../walk'
43

5-
export const createScopedContext = (ctx: Context, data: object): Context => {
6-
if (isObject(data)) {
7-
const parentScope = ctx.scope
8-
const mergedScope = Object.create(parentScope)
9-
Object.defineProperties(mergedScope, Object.getOwnPropertyDescriptors(data))
10-
const proxy = new Proxy(mergedScope, {
11-
set(target, key, val, receiver) {
12-
// when setting a property that doesn't exist on current scope,
13-
// do not create it on the current scope and fallback to parent scope.
14-
if (receiver === reactiveProxy && !target.hasOwnProperty(key)) {
15-
return Reflect.set(parentScope, key, val)
16-
}
17-
return Reflect.set(target, key, val, receiver)
4+
export const createScopedContext = (ctx: Context, data = {}): Context => {
5+
const parentScope = ctx.scope
6+
const mergedScope = Object.create(parentScope)
7+
Object.defineProperties(mergedScope, Object.getOwnPropertyDescriptors(data))
8+
mergedScope.$refs = Object.create(parentScope.$refs)
9+
const proxy = new Proxy(mergedScope, {
10+
set(target, key, val, receiver) {
11+
// when setting a property that doesn't exist on current scope,
12+
// do not create it on the current scope and fallback to parent scope.
13+
if (receiver === reactiveProxy && !target.hasOwnProperty(key)) {
14+
return Reflect.set(parentScope, key, val)
1815
}
19-
})
20-
const reactiveProxy = reactive(proxy)
21-
return {
22-
...ctx,
23-
scope: reactiveProxy
16+
return Reflect.set(target, key, val, receiver)
2417
}
18+
})
19+
const reactiveProxy = reactive(proxy)
20+
return {
21+
...ctx,
22+
scope: reactiveProxy
2523
}
26-
return ctx
2724
}

tests/ref.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@
1414
change dynamicRef
1515
</button>
1616
<button @click="show = !show">toggle</button>
17+
18+
<div v-scope>
19+
<p ref="x">nested scope ref</p>
20+
<button
21+
@click="console.log({ x: $refs.x, y: $refs.y, input: $refs.input })"
22+
>
23+
log nested scope refs
24+
</button>
25+
</div>
1726
</div>

0 commit comments

Comments
 (0)