Skip to content

Commit 6930e60

Browse files
committedApr 16, 2024··
fix(runtime-core): fix regression for $attrs tracking in slots
close #10710
1 parent 97716ae commit 6930e60

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
 

‎packages/runtime-core/__tests__/rendererAttrsFallthrough.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
render,
2121
withModifiers,
2222
} from '@vue/runtime-dom'
23+
import { createApp } from 'vue'
2324
import { PatchFlags } from '@vue/shared'
2425

2526
describe('attribute fallthrough', () => {
@@ -783,4 +784,31 @@ describe('attribute fallthrough', () => {
783784
expect(textBar).toBe('from GrandChild')
784785
expect(textFoo).toBe('from Child')
785786
})
787+
788+
// covers uncaught regression #10710
789+
it('should track this.$attrs access in slots', async () => {
790+
const GrandChild = {
791+
template: `<slot/>`,
792+
}
793+
const Child = {
794+
components: { GrandChild },
795+
template: `<div><GrandChild>{{ $attrs.foo }}</GrandChild></div>`,
796+
}
797+
798+
const obj = ref(1)
799+
const App = {
800+
render() {
801+
return h(Child, { foo: obj.value })
802+
},
803+
}
804+
805+
const root = document.createElement('div')
806+
createApp(App).mount(root)
807+
808+
expect(root.innerHTML).toBe('<div foo="1">1</div>')
809+
810+
obj.value = 2
811+
await nextTick()
812+
expect(root.innerHTML).toBe('<div foo="2">2</div>')
813+
})
786814
})

‎packages/runtime-core/src/componentPublicInstance.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,10 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
368368
// public $xxx properties
369369
if (publicGetter) {
370370
if (key === '$attrs') {
371-
track(instance, TrackOpTypes.GET, key)
371+
track(instance.attrs, TrackOpTypes.GET, '')
372372
__DEV__ && markAttrsAccessed()
373373
} else if (__DEV__ && key === '$slots') {
374+
// for HMR only
374375
track(instance, TrackOpTypes.GET, key)
375376
}
376377
return publicGetter(instance)

0 commit comments

Comments
 (0)
Please sign in to comment.