mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
fix(nuxt): don't overwrite existing scope in runWithContext
(#26976)
This commit is contained in:
parent
7ea05acbc9
commit
ea21feaaf4
@ -378,9 +378,7 @@ export function useAsyncData<
|
|||||||
const hasScope = getCurrentScope()
|
const hasScope = getCurrentScope()
|
||||||
if (options.watch) {
|
if (options.watch) {
|
||||||
const unsub = watch(options.watch, () => asyncData.refresh())
|
const unsub = watch(options.watch, () => asyncData.refresh())
|
||||||
if (instance) {
|
if (hasScope) {
|
||||||
onUnmounted(unsub)
|
|
||||||
} else if (hasScope) {
|
|
||||||
onScopeDispose(unsub)
|
onScopeDispose(unsub)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -389,9 +387,7 @@ export function useAsyncData<
|
|||||||
await asyncData.refresh()
|
await asyncData.refresh()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (instance) {
|
if (hasScope) {
|
||||||
onUnmounted(off)
|
|
||||||
} else if (hasScope) {
|
|
||||||
onScopeDispose(off)
|
onScopeDispose(off)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { effectScope, getCurrentInstance, hasInjectionContext, reactive } from 'vue'
|
import { effectScope, getCurrentInstance, getCurrentScope, hasInjectionContext, reactive } from 'vue'
|
||||||
import type { App, EffectScope, Ref, VNode, onErrorCaptured } from 'vue'
|
import type { App, EffectScope, Ref, VNode, onErrorCaptured } from 'vue'
|
||||||
import type { RouteLocationNormalizedLoaded } from '#vue-router'
|
import type { RouteLocationNormalizedLoaded } from '#vue-router'
|
||||||
import type { HookCallback, Hookable } from 'hookable'
|
import type { HookCallback, Hookable } from 'hookable'
|
||||||
@ -255,7 +255,7 @@ export function createNuxtApp (options: CreateOptions) {
|
|||||||
data: {},
|
data: {},
|
||||||
},
|
},
|
||||||
runWithContext (fn: any) {
|
runWithContext (fn: any) {
|
||||||
if (nuxtApp._scope.active) {
|
if (nuxtApp._scope.active && !getCurrentScope()) {
|
||||||
return nuxtApp._scope.run(() => callWithNuxt(nuxtApp, fn))
|
return nuxtApp._scope.run(() => callWithNuxt(nuxtApp, fn))
|
||||||
}
|
}
|
||||||
return callWithNuxt(nuxtApp, fn)
|
return callWithNuxt(nuxtApp, fn)
|
||||||
|
@ -2605,3 +2605,13 @@ describe('lazy import components', () => {
|
|||||||
expect(html).toContain('lazy-named-comp-server')
|
expect(html).toContain('lazy-named-comp-server')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('defineNuxtComponent watch duplicate', () => {
|
||||||
|
it('test after navigation duplicate', async () => {
|
||||||
|
const { page } = await renderPage('/define-nuxt-component')
|
||||||
|
await page.getByTestId('define-nuxt-component-bar').click()
|
||||||
|
await page.getByTestId('define-nuxt-component-state').click()
|
||||||
|
await page.getByTestId('define-nuxt-component-foo').click()
|
||||||
|
expect(await page.getByTestId('define-nuxt-component-state').first().innerText()).toBe('2')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
29
test/fixtures/basic/pages/define-nuxt-component/index.vue
vendored
Normal file
29
test/fixtures/basic/pages/define-nuxt-component/index.vue
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export default defineNuxtComponent({
|
||||||
|
name: 'DefineNuxtComponentTest',
|
||||||
|
setup () {
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
return {
|
||||||
|
router,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
data-testid="define-nuxt-component-bar"
|
||||||
|
@click="router.push('/define-nuxt-component/nested/bar')"
|
||||||
|
>
|
||||||
|
Open bar
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
data-testid="define-nuxt-component-foo"
|
||||||
|
@click="router.push('/define-nuxt-component/nested/foo')"
|
||||||
|
>
|
||||||
|
Open foo
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
34
test/fixtures/basic/pages/define-nuxt-component/nested/[foo].vue
vendored
Normal file
34
test/fixtures/basic/pages/define-nuxt-component/nested/[foo].vue
vendored
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export default defineNuxtComponent({
|
||||||
|
name: 'DefineNuxtComponentTest',
|
||||||
|
setup () {
|
||||||
|
const state = useState('define-nuxt-component-counter', () => 0)
|
||||||
|
const watcher = useState('define-nuxt-component-watcher', () => 0)
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
// Should trigger once per page on state change
|
||||||
|
watch(state, () => {
|
||||||
|
watcher.value++
|
||||||
|
})
|
||||||
|
|
||||||
|
state.value++
|
||||||
|
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
watcher,
|
||||||
|
router,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
data-testid="define-nuxt-component-state"
|
||||||
|
@click="router.push('/define-nuxt-component')"
|
||||||
|
>
|
||||||
|
{{ watcher }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
Loading…
Reference in New Issue
Block a user