Skip to content

Commit

Permalink
fix(runtime-core): fix regression for $attrs tracking in slots
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and lynxlangya committed May 30, 2024
1 parent 0f3eb21 commit 3081008
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/runtime-core/__tests__/rendererAttrsFallthrough.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
render,
withModifiers,
} from '@vue/runtime-dom'
import { createApp } from 'vue'
import { PatchFlags } from '@vue/shared'

describe('attribute fallthrough', () => {
Expand Down Expand Up @@ -783,4 +784,31 @@ describe('attribute fallthrough', () => {
expect(textBar).toBe('from GrandChild')
expect(textFoo).toBe('from Child')
})

// covers uncaught regression #10710
it('should track this.$attrs access in slots', async () => {
const GrandChild = {
template: `<slot/>`,
}
const Child = {
components: { GrandChild },
template: `<div><GrandChild>{{ $attrs.foo }}</GrandChild></div>`,
}

const obj = ref(1)
const App = {
render() {
return h(Child, { foo: obj.value })
},
}

const root = document.createElement('div')
createApp(App).mount(root)

expect(root.innerHTML).toBe('<div foo="1">1</div>')

obj.value = 2
await nextTick()
expect(root.innerHTML).toBe('<div foo="2">2</div>')
})
})
3 changes: 2 additions & 1 deletion packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,10 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
// public $xxx properties
if (publicGetter) {
if (key === '$attrs') {
track(instance, TrackOpTypes.GET, key)
track(instance.attrs, TrackOpTypes.GET, '')
__DEV__ && markAttrsAccessed()
} else if (__DEV__ && key === '$slots') {
// for HMR only
track(instance, TrackOpTypes.GET, key)
}
return publicGetter(instance)
Expand Down

0 comments on commit 3081008

Please sign in to comment.