Skip to content

Commit 980daf5

Browse files
author
wusaby-rush
committed
Change scope of ref with v-scope
1 parent b66a6dd commit 980daf5

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/walk.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const modifierRE = /\.([\w-]+)/g
1515
export let inOnce = false
1616

1717
export const walk = (node: Node, ctx: Context): ChildNode | null | void => {
18+
const parentCtx = ctx
1819
const type = node.nodeType
1920
if (type === 1) {
2021
// Element
@@ -55,6 +56,9 @@ export const walk = (node: Node, ctx: Context): ChildNode | null | void => {
5556

5657
// ref
5758
if ((exp = checkAttr(el, 'ref'))) {
59+
if (ctx !== parentCtx) {
60+
applyDirective(el, ref, `"${exp}"`, parentCtx)
61+
}
5862
applyDirective(el, ref, `"${exp}"`, ctx)
5963
}
6064

tests/ref.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
id="root"
88
ref="root"
99
v-scope="{ dynamicRef: 'x', show: true }"
10-
v-effect="console.log({ x: $refs.x, y: $refs.y, input: $refs.input })"
10+
v-effect="console.log({ x: $refs.x, y: $refs.y, input: $refs.input, modal: $refs.modal })"
1111
>
1212
<p>Accessing root el (with ref): id is {{ $refs.root.id }}</p>
1313
<p>Accessing root el (with $root): id is {{ $refs.root.id }}</p>
@@ -20,10 +20,10 @@
2020
</button>
2121
<button @click="show = !show">toggle</button>
2222

23-
<div v-scope>
23+
<div id="modal" ref="modal" v-scope>
2424
<p ref="x">nested scope ref</p>
2525
<button
26-
@click="console.log({ x: $refs.x, y: $refs.y, input: $refs.input })"
26+
@click="console.log({ x: $refs.x, y: $refs.y, input: $refs.input, modal: $refs.modal })"
2727
>
2828
log nested scope refs
2929
</button>

0 commit comments

Comments
 (0)