📜  VueJS 中创建事件和挂载事件有什么区别?

📅  最后修改于: 2022-05-13 01:58:10.363000             🧑  作者: Mango

VueJS 中创建事件和挂载事件有什么区别?

VueJS是一个模型-视图-ViewModel JavaScript 框架,用于构建用户界面和单页应用程序。它有几个生命周期钩子(不超过 9 个)。在本文中,我们将区分作为组件生命周期一部分的两种类型的事件。

  • 已创建
  • 已安装

在这两者中,mounted hooks 也被称为最常用的 hooks,因为它在不涉及“服务器端渲染”概念的情况下很容易处理,而 created 在我们处理服务器和从后端 API 获取数据时更有用.

Created() 和 Mounted() 事件之间的区别:-

CategoryCreatedMounted
StageIt occurs at the earliest stage of the Vue lifecycle.It occurs after the created hook is called. 
OccurrenceIt occurs only once in the lifecycle of component.It can occur more than once in the lifecycle of component.
AccessIt has access to component’s data, methods, mounting, and computer properties.You need to access (or) modify the DOM of your component immediately before (or) after initial rendering.
DOMIt cannot perform DOM manipulation because event is not mounted.It can perform DOM manipulation because event is already mounted
API’SGenerally, used for fetching data from backend API.In mounted, we need to perform tasks like fetching data from API in created hook only.
Mounting PhaseMounting phase is not started by this time and $el property is not available.Here, in the mounting phase, every time component is loaded to $el. 
Child ComponentIf the component has child elements then it is certain that even child component will have created hook initialized.If component has child elements then there is no guarantee all the child components will be mounted.
Server-SideThis hook is especially called when we want to read data from server.This hook is not called during server-side rendering.