search
HomeWeb Front-endVue.jsA brief analysis of life cycle and data sharing in Vue

This article will take you to continue learningVue, and introduce in detail the life cycle and data sharing in the essential knowledge for getting started with Vue. I hope it will be helpful to everyone!

A brief analysis of life cycle and data sharing in Vue

1. Component life cycle

##1.1 Life cycle & life cycle function

1️⃣ Life Cycle refers to the entire stage of a component from

creation → running → destruction , emphasizes a time period . (Learning video sharing: vue video tutorial)

2️⃣ Life cycle function: It is a

built-in function provided by the vue framework, which will accompany the life cycle of the component, Automatically execute in order.

? Warm reminder?:

Life cycle emphasizes time period, lifecycle function emphasizes time point.

1.2 Classification of component life cycle functions

A brief analysis of life cycle and data sharing in Vue

1.3 Life cycle diagram Indication

You can refer to the "life cycle diagram" given in vue official documentation to further understand the process of component life cycle execution:

https://cn.vuejs.org/ v2/guide/instance.html#Life cycle diagram
A brief analysis of life cycle and data sharing in Vue


2. Data sharing between components

2.1 Relationship between components

In project development, the most common relationships between components are divided into the following two types:

  • Father-son relationship

  • Brother relationship

2.2 Data sharing between parent and child components

Data sharing between parent and child components is divided into:

  • Parent → Child Shared data

  • Child → ParentShared data

2.2.1 Parent component to child Components share data

To share data from a parent component to a child component, you need to use

custom attributes. The sample code is as follows:
A brief analysis of life cycle and data sharing in Vue

2.2.2 Child component shares data with parent component

Uses when child component shares data with parent component

Custom events. The sample code is as follows:
A brief analysis of life cycle and data sharing in Vue

2.3 Data sharing between sibling components

In vue2.x, sibling components The data sharing scheme between them is

EventBus.
A brief analysis of life cycle and data sharing in Vue ?Steps for using EventBus?:

    Create the
  1. eventBus.js module and share a Vue instance object externally;
  2. On the data sender, call the
  3. bus.$emit('event name', data to be sent) method to trigger the custom event;
  4. On the data receiver, call
  5. bus.$on('Event name', event handling function) method registers a custom event;

3. ref reference

3.1 What is ref reference

1️⃣ ref is used to assist developers to obtain DOM without

not relying on jQuery. A reference to an element or component. 2️⃣ Each vue component instance contains a

$refs object

, which stores references to the corresponding DOM elements or components. By default, component's $refs point to an empty object . The code demonstration is as follows:


A brief analysis of life cycle and data sharing in Vue

3.2 Use ref to reference the DOM elementIf you want to use If ref refers to a DOM element on the page, it can be operated as follows:

The code is demonstrated as follows:
A brief analysis of life cycle and data sharing in Vue

##3.3 Use ref to reference component instances

If you want to use

ref To reference the component instance on the page, you can operate as follows:
A brief analysis of life cycle and data sharing in Vue

3.4 Control the on-demand switching of text boxes and buttons

Use Boolean value

inputVisible to control the on-demand switching of the text box and button in the component. The sample code is as follows:
A brief analysis of life cycle and data sharing in Vue

3.5 Let the text box automatically gain focus

After the text box is displayed, if you want If it gets focus immediately, you can add a ref reference to it and call the

.focus() method of the native DOM object. The sample code is as follows:
A brief analysis of life cycle and data sharing in Vue

##3.6 this.$nextTick(cb) method

$nextTick of the component The (cb)

method will defer the cb callback to execution after the next DOM update cycle. The popular understanding is: wait for the DOM update of the component to be completed before executing the cb callback function. This ensures that the cb callback function can operate on the latest DOM elements.
A brief analysis of life cycle and data sharing in Vue (Learning video sharing:

web front-end development

, Basic programming video)

The above is the detailed content of A brief analysis of life cycle and data sharing in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
Vue常见面试题汇总(附答案解析)Vue常见面试题汇总(附答案解析)Apr 08, 2021 pm 07:54 PM

本篇文章给大家分享一些Vue面试题(附答案解析)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

5 款适合国内使用的 Vue 移动端 UI 组件库5 款适合国内使用的 Vue 移动端 UI 组件库May 05, 2022 pm 09:11 PM

本篇文章给大家分享5 款适合国内使用的 Vue 移动端 UI 组件库,希望对大家有所帮助!

vue中props可以传递函数吗vue中props可以传递函数吗Jun 16, 2022 am 10:39 AM

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

手把手带你利用vue3.x绘制流程图手把手带你利用vue3.x绘制流程图Jun 08, 2022 am 11:57 AM

利用vue3.x怎么绘制流程图?下面本篇文章给大家分享基于 vue3.x 的流程图绘制方法,希望对大家有所帮助!

聊聊vue指令中的修饰符,常用事件修饰符总结聊聊vue指令中的修饰符,常用事件修饰符总结May 09, 2022 am 11:07 AM

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?React和Vue项目的解决方法浅析如何覆盖组件库样式?React和Vue项目的解决方法浅析May 16, 2022 am 11:15 AM

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

通过9个Vue3 组件库,看看聊前端的流行趋势!通过9个Vue3 组件库,看看聊前端的流行趋势!May 07, 2022 am 11:31 AM

本篇文章给大家分享9个开源的 Vue3 组件库,通过它们聊聊发现的前端的流行趋势,希望对大家有所帮助!

react与vue的虚拟dom有什么区别react与vue的虚拟dom有什么区别Apr 22, 2022 am 11:11 AM

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment