Vue.js learning and common knowledge (1)


Vue.js is a popular JavaScript framework for building user interfaces. Vue.js has the characteristics of ease of use, strong flexibility, good scalability, and complete documentation, so it is widely used in front-end development. The following will explain in detail the basic concepts, components, instructions, computed properties, listeners, and life cycles of Vue.js.

1. Basic concepts

1.1 Introduction to Vue.js

Vue.js is a popular JavaScript framework for building user interfaces. Vue.js has the characteristics of ease of use, strong flexibility, good scalability, and complete documentation, so it is widely used in front-end development. Vue.js was created by Chinese programmer You Yuxi in 2014 and released Vue 3.0 version in 2018. The latest version of Vue.js is Vue 4.0, which will be released in 2022.

1.2 Features of Vue.js

Vue.js has the following characteristics:
(1) Ease of use: Vue.js has a shallow learning curve and is easy to use. It provides some simple syntax and API, allowing developers to quickly build applications.
(2) Strong flexibility: Vue.js allows developers to choose different components, instructions and plug-ins according to their needs, so as to realize highly customized applications.
(3) Good scalability: Vue.js provides a wealth of plug-ins and tools that can help developers quickly expand the functionality and performance of applications.
(4) Complete documentation: The official documentation of Vue.js is detailed and comprehensive, which can help developers quickly understand the usage and principles of Vue.js.

1.3 Application scenarios of Vue.js

Vue.js is suitable for the following application scenarios:
(1) Building single-page applications (SPA): Vue.js can help developers quickly build responsive, interactive and rich single-page applications.
(2) Building a management system: Vue.js can be used to build an enterprise-level management system, such as CRM, ERP, etc.
(3) Build games: Vue.js can be used to build web games, such as role-playing games, strategy games, etc.
(4) Building mobile applications: Vue.js can help developers build mobile applications, such as iOS and Android applications.

2. Components

2.1 What are components?

Component is a concept in Vue.js, and it is one of the core functions of Vue.js. A component is a reusable and composable UI element that can contain HTML, CSS, JavaScript, and more. Components can be encapsulated into a single file for easy reuse in different projects.

2.2 What is the syntax of the component?

The syntax of the component is as follows:

<template>  
 <div>  
   <!-- 组件的 HTML 内容 -->  
 </div>  
</template>
<script>  
export default {
      
        
 name: 'MyComponent',  
 // 组件的选项  
};  
</script>
<style scoped>  
/* 组件的 CSS 内容 */  
</style>  

Among them, <template>the element is used to define the HTML content of the component, <script>the element is used to define the JavaScript code of the component, and <style>the element is used to define the CSS content of the component. export defaultUsed to export components, namethe attribute is used to specify the name of the component.

2.3 What is the life cycle of components?

The life cycle of a component refers to a series of events experienced by the component from creation to destruction. The life cycle of a component consists of the following three phases:

  • Create phase: including beforeCreate, created, beforeMount, mounted and other events.
  • Update phase: including beforeUpdate, updated, beforeDestroy, destroyed and other events.
  • Destruction phase: including beforeUnmount, unmounted and other events.

2.4 What is the data transfer of components?

Component data transfer refers to the process in which a parent component transfers data to a child component, or a child component transfers data to a parent component. In Vue.js, the parent component can pass data to the child component through the props attribute, and the child component can pass data to the parent component through the $emit event.

3. Instructions

3.1 What is an order?

Directives are a special syntax in Vue.js for adding custom behavior in components. A directive is an object that contains a bindmethod and a updatemethod. bindThe method is used to bind the directive to the component, and updatethe method is used to update the state of the directive.

3.2 What is the syntax of the command?

The syntax of the directive is as follows:

<template>  
 <div>  
   <!-- 组件的 HTML 内容 -->  
 </div>  
</template>
<script>  
export default {
      
        
 name: 'MyComponent',  
 // 组件的选项  
};  
</script>
<style scoped>  
/* 组件的 CSS 内容 */  
</style>
<script>  
export default {
      
        
 name: 'MyDirective',  
 // 指令的选项  
};  
</script>
<template>  
 <div>  
   <!-- 指令的 HTML 内容 -->  
 </div>  
</template>
<style scoped>  
/* 指令的 CSS 内容 */  
</style>  

Among them, <template>the element is used to define the HTML content of the instruction, <script>the element is used to define the JavaScript code of the instruction, and <style>the element is used to define the CSS content of the instruction. <template>HTML content within an element can contain special syntax for directives, such as v-if, v-show, v-else, v-for, v-bind, v-onetc. These special syntaxes will be parsed by Vue.js and bind corresponding behaviors to the directives. In <script>the element, you can define the options of the command, such as the name, version, author and other information of the command, as well as the specific implementation of the command. A directive's implementation typically includes a bindmethod and a updatemethod to bind the directive to the component and update the directive's state. In <style>the element, you can define the CSS content of the directive, such as styles, layouts, etc. These CSS contents will only be applied to the component where the directive is located.

4. Calculated properties

4.1 Overview of Computed Properties

Computed properties in Vue.js are a special kind of properties that are used to calculate the data of a component. Computed properties can help developers simplify code and improve the readability of components.

4.2 Implementation of Computed Properties

Implementations of Vue.js computed properties typically include a getmethod and a setmethod for getting and setting the state of the computed property. getThe method is used to get the state of the computed property, and setthe method is used to set the state of the computed property.

Five, listener

5.1 Listener overview

The listener of Vue.js is a special syntax used to monitor data changes of components. Listeners can be used to monitor component property changes, method calls, event triggers, etc. Vue.js provides many built-in listeners, such as v-on, v-bind, v-show, v-ifetc. These listeners can easily monitor the data changes of components. In addition, Vue.js also allows developers to customize listeners to better meet the needs of the application.

5.2 Implementation of Listener

A Vue.js listener implementation typically includes a bindmethod and a updatemethod to bind the listener to the component and update the listener's state. bindThe method is used to bind the listener to the component's template, and updatethe method is used to update the state of the listener.

6. Life cycle

6.1 Life cycle overview

The life cycle of Vue.js is a special syntax used to control the life cycle of components. The life cycle can be used to initialize components, mount components, update components, destroy components, etc. Vue.js provides many built-in lifecycles, such as beforeCreate, created, beforeMount, mounted, beforeUpdate, updated, beforeDestroyand , destroyedetc., which can easily control the lifecycle of components. In addition, Vue.js also allows developers to customize the life cycle to better meet the needs of the application.

6.2 Realization of life cycle

The implementation of the Vue.js life cycle usually includes a beforeCreatemethod, a method, a createdmethod, a beforeMountmethod, a mountedmethod, a beforeUpdatemethod updated, a beforeDestroymethod, and a destroyedmethod, which are used to initialize components, mount components, update components, destroy components, and so on.

7. The difference between instructions and listeners

7.1 The difference between instructions and listeners

Both directives and listeners are special syntax used in Vue.js to control the behavior of components, but they have some differences.
(1) Directives are used to control the behavior of components, such as display, hide, update, bind data, etc. Directives are usually used in component templates, they can conveniently control the behavior of components.
(2) Listeners are used to monitor component data changes, such as property changes, method calls, event triggers, etc. Listeners are usually used in the JavaScript code of components, and they can easily monitor the data changes of components.
(3) Both instructions and listeners can be used to control the behavior of components, but instructions focus more on the view level of components, while listeners focus more on the logic level of components.

7.2 Usage Scenarios of Commands and Listeners

Directives and listeners are usually used in different scenarios.
(1) Directives are usually used to control the view level of components, such as display, hide, update, bind data, etc. Directives are usually used in component templates, they can conveniently control the behavior of components.
(2) Listeners are usually used to monitor component data changes, such as property changes, method calls, event triggers, etc. Listeners are usually used in the JavaScript code of components, and they can easily monitor the data changes of components.
(3) In some complex scenarios, instructions and listeners may be used at the same time to achieve more fine-grained component control.

8. The difference between computed properties and listeners

8.1 The difference between computed properties and listeners

Both computed properties and listeners are special syntax in Vue.js for listening to data changes, but they have some differences.
(1) Calculated properties are used to monitor data changes and return a new value, such as calculating a new value based on a certain property value. Computed properties are often used in component templates, they can easily listen to data changes and return new values.
(2) The listener is used to monitor data changes and execute corresponding callback functions, such as executing a function when an attribute changes. Listeners are usually used in JavaScript code of components, they can easily monitor data changes and execute corresponding callback functions.
(3) Both computed properties and listeners can be used to monitor data changes, but computed properties focus more on returning a new value, while listeners focus more on executing corresponding callback functions.

8.2 Computed properties and listeners usage scenarios

Computed properties and listeners are often used in different scenarios.
(1) Computed properties are usually used to monitor data changes and return a new value, such as calculating a new value based on a certain property value. Computed properties are often used in a component's template so that they are automatically updated when the component renders. For example, there is a computed property fullNamethat computes a full name from the firstNameand properties. Automatically updates lastNamewhen firstNameor lastNamechanges . (2) Listeners are usually used to monitor data changes and execute corresponding callback functions, such as executing a function when a property changes. Listeners are usually used in JavaScript code of components, they can easily monitor data changes and execute corresponding callback functions. For example, there is a listener that listens for property changes and executes a callback function when it changes. Called automatically when changes occur . In addition to computed properties and listeners, Vue.js also provides some other special syntax, such as , , , etc., which can be used to monitor data changes of components. Developers can choose the appropriate special syntax according to specific needs, so as to better realize the functions of the application.fullName
fullNameChangedfullNamefullNamefullNameChanged
v-onv-bindv-showv-if

Guess you like

Origin blog.csdn.net/superdangbo/article/details/132001694