前端开发——Vue 监听组件生命周期

监听组件生命周期

通常我们使用 $emit 监听组件生命周期,父组件接收事件进行通知。

子组件

export default {
    mounted() {
        this.$emit( listenMounted )
    }
}

父组件

<template>
    <div>
        <List @listenMounted="listenMounted" />
    </div>
</template>

其实有一种简单的方法就是使用@hook 来监听组件的生命周期,而不需要在组件内部做任何改动。同样,创建、更新等也可以使用这个方法。

<template>
    <List @hook:mounted="listenMounted" />
</template>

猜你喜欢

转载自blog.csdn.net/helloyangkl/article/details/129078194