vue keep-alive 生命周期

Posted mengfangui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue keep-alive 生命周期相关的知识,希望对你有一定的参考价值。

keep-alive 生命周期

父组件

<!--  -->
<template>
    <div class="">
        <keep-alive>
            <testa v-if="test"></testa>
            <testb v-else></testb>
        </keep-alive>
        <button @click="change">test change</button>
    </div>
</template>

<script>
import testa from "./testA";
import testb from "./testB";
export default {
    components: { testa,testb, },
    data() {
        return {
            test:true
        };
    },
    computed: {},
    watch: {
        name: {
            handler(val, oldVal) {
                /* ... */
            },
            deep: true,
            immediate: true
        }
    },
    methods: {
        change(){
            this.test = !this.test;
        }
    }
};
</script>

组件A:

<!--  -->
<template>
    <div class="">
        about-componnet
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        return {};
    },
    computed: {},
    watch: {
        name: {
            handler(val, oldVal) {
                /* ... */
            },
            deep: true,
            immediate: true
        }
    },
    methods: {},
    beforeCreate() {
        console.log("A--beforeCreate");
    },
    created() {
        console.log("A--created");
    },
    beforeMount() {
        console.log("A--beforeMount");
    },
    mounted() {
        console.log("A--mounted");
    },
    beforeDestroy() {
        console.log("A--beforeDestroy");
    },
    activated() {
        console.log("A--activated");
    },
    deactivated() {
        console.log("A--deactivated");
    }
};
</script>

组件B:

<!--  -->
<template>
    <div class="">
        about-componnet
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        return {};
    },
    computed: {},
    watch: {
        name: {
            handler(val, oldVal) {
                /* ... */
            },
            deep: true,
            immediate: true
        }
    },
    methods: {},
    beforeCreate() {
        console.log("B--beforeCreate");
    },
    created() {
        console.log("B--created");
    },
    beforeMount() {
        console.log("B--beforeMount");
    },
    mounted() {
        console.log("B--mounted");
    },
    beforeDestroy() {
        console.log("B--beforeDestroy");
    },
    activated() {
        console.log("B--activated");
    },
    deactivated() {
        console.log("B--deactivated");
    }
};
</script>

切换组件A和组件B,控制台打印:

// A组件首次激活
A--beforeCreate
A--created
A--beforeMount
A--mounted
A--activated

// B组件首次激活,A组件失活
B--beforeCreate
B--created
B--beforeMount
A--deactivated
B--mounted
B--activated

// B组件失活,A组件激活
B--deactivated
A--activated

// A组件失活,B组件激活
A--deactivated
B--activated

结论:

  • 首次激活时,activated都在mounted后。
  • 再次激活时,本组件只走activated(另外一组件先失活deactivated)
  • 组件失活时,均不走beforeDestroy和destroyed

以上是关于vue keep-alive 生命周期的主要内容,如果未能解决你的问题,请参考以下文章

Vue的钩子函数[路由导航keep-alive生命周期钩子]

Vue的钩子函数[路由导航守卫keep-alive生命周期钩子]

vue----keep-alive缓存,activated,deactivated两个生命周期函数

vue隐藏页面触发的生命周期

vue的activated和deactivated生命周期

vue 生命周期函数