Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isFunction } from '@vue/shared'
import { currentInstance, getCurrentGenericInstance } from './component'
import { currentApp } from './apiCreateApp'
import { warn } from './warning'
import { isHmrUpdating } from './hmr'

interface InjectionConstraint<T> {}

Expand All @@ -12,7 +13,7 @@ export function provide<T, K = InjectionKey<T> | string | number>(
value: K extends InjectionKey<infer V> ? V : T,
): void {
if (__DEV__) {
if (!currentInstance || currentInstance.isMounted) {
if (!currentInstance || (currentInstance.isMounted && !isHmrUpdating)) {
warn(`provide() can only be used inside setup().`)
}
}
Expand Down
37 changes: 37 additions & 0 deletions packages/runtime-vapor/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
onDeactivated,
onMounted,
onUnmounted,
provide,
ref,
toDisplayString,
} from '@vue/runtime-dom'
Expand Down Expand Up @@ -1064,6 +1065,42 @@ describe('hot module replacement', () => {
)
})

test('reload setup only component', async () => {
const childId = 'test-child-reload-01'
const Child = defineVaporComponent({
__hmrId: childId,
render: compileToFunction(`<div>foo</div>`),
})
createRecord(childId, Child as any)

// Simulate router-view, it does not have a render function,
// so when it rerenders, the setup function will be called
const RouterView = defineVaporComponent({
setup() {
provide('foo', 'bar')
return createComponent(Child)
},
})

const { html } = define({
setup() {
return createComponent(RouterView)
},
}).render()

expect(html()).toBe('<div>foo</div>')

// will trigger parent rerender
reload(childId, {
__hmrId: childId,
render: compileToFunction(`<div>bar</div>`),
})

await nextTick()
expect(html()).toBe('<div>bar</div>')
expect('provide() can only be used inside setup()').not.toHaveBeenWarned()
})

describe('switch vapor/vdom modes', () => {
test('vapor -> vdom', async () => {
const id = 'vapor-to-vdom'
Expand Down
Loading