原因: 动态组件未配置key值;如果不配置 key 值,当多个页面配置 keepAlive:true 时,在切换缓存页面时会报错 Uncaught (in promise) TypeError: parentComponent.ctx.deactivate is not a function。
解决办法:需要给动态组件配置 key 值,来表示组件的唯一性和对应关系。
原代码:
<keep-alive>
<component v-if="tabMenu.length" :is="tabMenu[currentActiveIndex]?.component" />
</keep-alive>
修改后的代码:
<keep-alive>
<component v-if="tabMenu.length" :is="tabMenu[currentActiveIndex]?.component" :key="currentActiveIndex" />
</keep-alive>
修改后运行不再报错