父组件:使用 <keep-alive>,
<keep-alive exclude="">
<router-view @public_footer="public_footer"></router-view>
</keep-alive>
<app-footer v-if="isFooter"></app-footer>
export default {
name: 'App',
data() {
return {
isFooter: true, //是否挂在此组件标志位
}
},
created() {
this.isLoaded = false;
},
components: {
'app-home': Home,
'app-footer': Footer //父组件挂载
},
methods: {
public_footer: function(flag) { 父组件v-on监听方法
this.isFooter = flag;
}
}
}
子组件:不需要挂载于父组件中的 子组件<app-footer v-if="isFooter"></app-footer> 子组件desroyed()无法执行,同时exclude不能解决
mounted() {
this.$emit("public_footer", false); //传递给父组件, isFooter为 true
},
destroyed() {
console.log(1111); //控制台无任何打印
this.$emit("public_footer", true) //没有传递给父组件, isFooter仍为 false
}
解决办法:deactivated(),activated()中执行相应事件
activated() { //挂载后
this.$emit("public_footer", false);
},
deactivated(){ //销毁后
console.log(1111); //打印 1111
this.$emit("public_footer", true) // isFooter被修改为 true
}