vue.js - beforeDestroy vue动态组件
问题描述
如何在切换组件之前添加警报消息?(在路径改变之前,beforeDestroy 不起作用......)
<template>
<keep-alive>
<component
:is="dynamicComponent"
/>
</keep-alive>
</template>
解决方案
正如您已经发现的那样,当您保持组件处于活动状态时,它不会像普通组件一样抛出 " beforeDestroy
" 和 " created
",因为它保持活动状态。
因此,Vue 为此定义了其他生命周期方法:
activated
keep-alive
- 这在加载组件时被调用deactivated
<keep-alive>
-卸载组件时调用
你像这样使用它们:
export default {
created() {
console.log('created');
},
activated() {
console.log('activated');
},
deactivated() {
console.log('deactivated');
},
beforeDestroy() {
console.log('beforeDestroy');
},
}
推荐阅读
- scala - Scala match 语句的两个条件
- java - 通过将单个字符短缺的大小减小到 1 字节(7 位)来缩短 UTF8 编码的 db 大小
- kotlin - 将父视图发送给子视图 (BottomSheetDialogFragment)
- macos - 如何在没有 hdiutil flatten 和 unflatten 动词的 macOS 11.0 Big Sur 上将软件许可条款添加到磁盘映像
- dart - 用户与 iframe 交互后,如何从 Angular Dart 中的 iframe 获取 URL?
- c - 程序必须将端口号作为 CmdLine Socket prog 的参数
- javascript - How to count the number of number-based characters in an input field?
- python - 获取固定大小的子列表
- azure-devops - 检测是否在管道文件和脚本中检查了“启用系统诊断”的条件
- python - 导入并行 python 模块。模块无法识别