--- title: KeepAlive Include/Exclude Requires Component Name impact: MEDIUM impactDescription: The include and exclude props match against component name option, which must be explicitly declared type: gotcha tags: [vue3, keepalive, component-name, include, exclude, sfc] --- # KeepAlive Include/Exclude Requires Component Name **Impact: MEDIUM** - When using `include` or `exclude` props on KeepAlive, the matching is done against the component's `name` option. Components without an explicit name will not match and caching behavior will be unexpected. ## Task Checklist - [ ] Declare `name` option on components used with include/exclude - [ ] Use `defineOptions({ name: '...' })` in ` ``` **Result:** TabA is NOT cached because it has no `name` option to match against. ## Solutions ### Solution 1: Use defineOptions (Vue 3.3+) ```vue ``` ### Solution 2: Dual Script Block ```vue ``` ### Solution 3: Rely on Auto-Inference (Vue 3.2.34+) Since Vue 3.2.34, SFCs using ` ``` ```vue ``` ## Common Mistakes ### Mistake 1: Name Doesn't Match ```vue ``` ```vue ``` **Fix:** Ensure names match exactly: ```vue ``` ### Mistake 2: Dynamic Components Without Names ```vue ``` **Fix:** Ensure the imported component has a name declared. ### Mistake 3: Using Props in Options API ```vue ``` ## Debugging Name Issues Check what name Vue sees for your component: ```vue ``` ## Using Different Match Formats ```vue ``` ## Key Points 1. **Name must match exactly** - Case-sensitive string matching 2. **Vue 3.2.34+ auto-infers name** - From filename for `