Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/async-component-keepalive-ref-issue.md
1# Async Components with keep-alive Ref Issues23## Rule45When using `<keep-alive>`, `<component>`, and `defineAsyncComponent` together, be aware that template refs can become undefined when the component is re-activated after being deactivated.67## Why This Matters89This is a known Vue issue where the ref binding works correctly on first activation but becomes undefined on subsequent activations. This can cause runtime errors when trying to access component methods or properties through refs.1011## Problem Scenario1213```vue14<script setup>15import { ref, defineAsyncComponent } from 'vue'1617const AsyncWidget = defineAsyncComponent(() =>18import('./Widget.vue')19)2021const currentComponent = ref(AsyncWidget)22const widgetRef = ref(null)2324function callWidgetMethod() {25// May be undefined after component reactivation!26widgetRef.value?.doSomething()27}28</script>2930<template>31<keep-alive>32<component :is="currentComponent" ref="widgetRef" />33</keep-alive>34</template>35```3637## Workarounds3839### Option 1: Use onActivated to re-establish ref access4041```vue42<script setup>43import { ref, defineAsyncComponent, onActivated, nextTick } from 'vue'4445const AsyncWidget = defineAsyncComponent(() =>46import('./Widget.vue')47)4849const currentComponent = ref(AsyncWidget)50const widgetRef = ref(null)5152// Use a computed or method that waits for ref to be available53async function callWidgetMethod() {54await nextTick()55if (widgetRef.value) {56widgetRef.value.doSomething()57}58}59</script>60```6162### Option 2: Avoid mixing all three patterns6364If possible, use one of these alternatives:6566```vue67<!-- Option A: Don't use keep-alive with async components -->68<template>69<component :is="currentComponent" ref="widgetRef" />70</template>7172<!-- Option B: Use static component with keep-alive -->73<script setup>74import Widget from './Widget.vue' // Regular import75</script>76<template>77<keep-alive>78<component :is="Widget" ref="widgetRef" />79</keep-alive>80</template>81```8283### Option 3: Use provide/inject instead of refs8485```vue86<!-- Parent.vue -->87<script setup>88import { provide, ref } from 'vue'8990const sharedState = ref({ /* shared data */ })91provide('widgetState', sharedState)92</script>9394<!-- Widget.vue (async component) -->95<script setup>96import { inject } from 'vue'97const widgetState = inject('widgetState')98</script>99```100101## Key Points1021031. This is a known issue when combining `<keep-alive>`, `<component :is>`, and `defineAsyncComponent`1042. Refs may become undefined after component deactivation/reactivation1053. Use `nextTick` and null checks when accessing refs1064. Consider alternative patterns like provide/inject for cross-component communication1075. Test thoroughly when using this combination108109## References110111- [Vue.js GitHub Discussion #11334](https://github.com/orgs/vuejs/discussions/11334)112- [Vue.js Async Components Documentation](https://vuejs.org/guide/components/async)113