简体   繁体   English

每个 vue.js 生命周期挂钩的用例是什么?

[英]What is the use case for each vue.js life cycle hooks?

So I know somethings about vue's life cycle hooks but for some of them I can't think of any real world use case or example that should be done in them and I think it might help me to better understand them by finding out their use case.所以我对 vue 的生命周期挂钩有所了解,但对于其中一些我想不出任何真实世界的用例或应该在其中完成的示例,我认为通过找出它们的用例可能会帮助我更好地理解它们.

here is what I know and don't know about them:以下是我对他们的了解和不了解:

Creation Hooks创建挂钩

  1. beforeCreate(): events and lifecycle have been initialized but data has not been made reactive --- use case?? beforeCreate():事件和生命周期已经初始化但数据还没有反应---用例?
  2. created(): you have access to data and events that are reactive but templates and virtual DOM have not yet been mounted or rendered --- use case: API calls created():您可以访问反应性数据和事件,但模板和虚拟 DOM 尚未安装或呈现 --- 用例:API 次调用

Mounting Hooks安装挂钩

  1. beforeMount(): runs before the initial render --- use case?? beforeMount():在初始渲染之前运行——用例??
  2. mounted(): you have access to the reactive component, templates and rendered DOM --- use case: modifying the DOM mounted():你可以访问反应组件、模板和呈现的 DOM --- 用例:修改 DOM

Updating Hooks更新挂钩

  1. beforeUpdate(): runs after data changes and before the DOM is re-rendered --- use case?? beforeUpdate():在数据更改之后和 DOM 重新呈现之前运行——用例?
  2. updated(): runs after data changes and the DOM is re-rendered --- use case?? updated():在数据更改和 DOM 重新呈现后运行——用例??

Destruction Hooks破坏钩子

  1. beforeDestroy(): runs before tear down --- use case: clean ups to avoid memory leak beforeDestroy():在拆卸之前运行 --- 用例:清理以避免 memory 泄漏
  2. destroyed(): runs after tear down --- use case?? destroyed():在拆除后运行——用例??

Thanks in advance to anyone helping me to understand these concepts better;)提前感谢任何帮助我更好地理解这些概念的人;)

According to official VueJS website:根据官方 VueJS 网站:

Each Vue instance goes through a series of initialization steps when it's created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes.每个 Vue 实例在创建时都会经历一系列初始化步骤——例如,它需要设置数据观察、编译模板、将实例挂载到 DOM 以及在数据发生变化时更新 DOM。 Along the way, it also runs functions called lifecycle hooks, giving users the opportunity to add their own code at specific stages.在此过程中,它还运行称为生命周期挂钩的功能,让用户有机会在特定阶段添加自己的代码。

在此处输入图像描述

Having that in mind, we have:考虑到这一点,我们有:

beforeCreate创建前

The beforeCreate hook runs at the very initialization of your component. beforeCreate挂钩在组件初始化时运行。 data has not been made reactive, and events have not been set up yet.数据还没有反应,事件还没有设置。

Usage用法

Using the beforeCreate hook is useful when you need some sort of logic/API call that does not need to be assigned to data.当您需要某种不需要分配给数据的逻辑/API 调用时,使用beforeCreate挂钩很有用。 Because if we were to assign something to data now, it would be lost once the state was initialized.因为如果我们现在要为数据分配一些东西,一旦 state 被初始化,它就会丢失。

created创建

You are able to access reactive data and events that are active with the created hook.您可以访问使用创建的挂钩激活的反应性数据和事件。 Templates and Virtual DOM have not yet been mounted or rendered.模板和虚拟 DOM 尚未安装或渲染。

Usage用法

Using the created method is useful when dealing with reading/writing the reactive data.在处理读取/写入反应数据时,使用created的方法很有用。 For example, if you want to make an API call and then store that value, this is the place to do it.例如,如果您想拨打 API 电话,然后存储该值,这就是执行此操作的地方。


The above are famously called as creation hooks, as opposed to mounting hooks.以上被称为creation挂钩,而不是mounting挂钩。

Mounting hooks are often the most used hooks.安装挂钩通常是最常用的挂钩。 They allow you to access your component immediately before and after the first render.它们允许您在第一次渲染之前和之后立即访问您的组件。 They do not, however, run during server-side rendering.但是,它们不会在服务器端呈现期间运行。

beforeMount挂载前

The beforeMount hook runs right before the initial render happens and after the template or render functions have been compiled. beforeMount挂钩在初始渲染发生之前和模板或渲染函数编译之后运行。

Usage用法

This is the last step you should perform your API calls before it's unnecessary late in the process because it's right after created — they have access to the same component variables.这是您应该执行 API 调用的最后一步,因为它就在创建之后就没有必要了,因为它们可以访问相同的组件变量。

mounted安装

In the mounted hook, you will have full access to the reactive component, templates, and rendered DOM (via this.$el).在挂载的钩子中,您将可以完全访问反应式组件、模板和呈现的 DOM(通过 this.$el)。

Usage用法

Use mounted for modifying the DOM—particularly when integrating non-Vue libraries.使用 mounted 修改 DOM——特别是在集成非 Vue 库时。


There also some hooks, which are called updating hooks.还有一些钩子,称为updating钩子。

Updating hooks are called whenever a reactive property used by your component changes or something else causes it to re-render.每当组件使用的响应式属性更改或其他原因导致它重新渲染时,都会调用更新挂钩。 They allow you to hook into the watch-compute-render cycle for your component.它们允许您挂接到组件的监视-计算-渲染周期。

Use updating hooks if you need to know when your component re-renders, perhaps for debugging or profiling.如果您需要知道组件何时重新渲染,可以使用更新挂钩,可能用于调试或分析。

There are:有:

beforeUpdate更新前

The beforeUpdate hook runs after data changes on your component and the update cycle begins, right before the DOM is patched and re-rendered. beforeUpdate挂钩在组件上的数据更改之后运行,并且更新周期开始,就在 DOM 被修补和重新呈现之前。

Usage用法

Use beforeUpdate if you need to get the new state of any reactive data on your component before it actually gets rendered.如果您需要在组件实际呈现之前获取组件上任何反应数据的新 state,请使用beforeUpdate

updated更新

The updated hook runs after data changes on your component and the DOM re-renders.更新的挂钩在组件上的数据更改和 DOM 重新呈现后运行。

Usage用法

Use updated if you need to access the DOM after a property change如果需要在属性更改后访问 DOM,请使用 updated


And last but not least, there are destruction hooks.最后但并非最不重要的是,有destruction挂钩。

Destruction hooks allow you to perform actions when your component is destroyed, such as cleanup or analytics sending.销毁挂钩允许您在组件被销毁时执行操作,例如清理或分析发送。 They fire when your component is being torn down and removed from the DOM.当您的组件被拆除并从 DOM 中移除时,它们会触发。

There are:有:

destroyed毁坏

By the time you reach the destroyed hook, there's practically nothing left on your component.当您到达 destroyed hook 时,您的组件上几乎没有任何东西。 Everything that was attached to it has been destroyed.附着在它上面的一切都被摧毁了。

Usage用法

Use destroyed if you need do any last-minute cleanup or inform a remote server that the component was destroyed如果您需要进行任何最后一分钟的清理或通知远程服务器组件已销毁,请使用 destroyed

beforeDestroy销毁前

beforeDestroy is fired right before teardown. beforeDestroy在拆解之前被触发。 Your component will still be fully present and functional.您的组件仍将完全存在并发挥作用。

Usage用法

Use beforeDestroy if you need to clean-up events or reactive subscriptions.如果您需要清理事件或响应式订阅,请使用beforeDestroy

This is the stage where you can do resource management, delete variables and clean up the component.这是您可以进行资源管理、删除变量和清理组件的阶段。


Keep in mind that these are the main lifecycle hooks and there are some other minor ones such as activated and deactivated that you can look into.请记住,这些是主要的生命周期挂钩,还有一些其他的小挂钩,例如activateddeactivated ,您可以查看。

Here is a link that might help you further down the line. 是一个链接,可以帮助您进一步了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM