Table of Contents
简单示例
全局注册
Home Web Front-end JS Tutorial What are vue components? Introduction to Vue components

What are vue components? Introduction to Vue components

Aug 07, 2018 pm 01:48 PM
javascript vue.js

What is the vue component? Components are reusable Vue instances. It is essentially an object that contains member attributes such as data, computed, watch, methods, filters, and vue component life cycle hooks. This article will give you a detailed introduction to the content of the vue component.

Let’s take a look at the component structure first:

{
  data(){
    return {
      //
    }
  },
  computed:{
    displayName(){
      return '';
    }
  },
  methods:{
    onClickHandler(params){
      // do something
    }
  }
}
Copy after login

Basic knowledge:

data attribute

The data attribute maintains the internal state of a component, and other components are not visible under normal circumstances.

Currently I don’t know how to monitor its changes;

Because the calculated attribute computed and the listening attribute watch can only monitor changes in responsive dependencies, and $refs is not responsive.

Can be passed to child components through props;

Can be passed to parent components through $emit;

Can be passed to this.$refs.ref.$data in mounted Get the internal state of the child component during the life cycle;

The data option of a component must be a function.

  data选项有两种定义方式:
  
  一、对象形式:
  
  ```
  data:{
    //引用该组件的地方,共用一个状态的引用,以至于,只要有一处修改了$data中的某一属性值,其它引用该组件的地方也跟随着改变该属性值(其实,不是跟随,本来就是同一个指向)。
  }
  ```
  
  二、函数形式:
  
  ```
  data(){
    return {
      //引用该组件的地方,每一个组件都会获得独立的引用,互不干扰。
    }
  }
  ```
Copy after login

computed attribute, methods attribute, filter

##Differencemethodcomputed filterTypeFunctionData variableFunctionPurposeUsed as event processing functionUsed as dataUsed as pipe symbol ScopeIntra-organizationIntra-organizationIntra-organization (local registration), global (global registration)ParameterCan take parametersWithout parameters (non-function)With parametersReturn valueNo Requirementsmust havemust havetriggeredtriggered when interactingin its related dependencies It will be re-evaluated only when changes occurExecuted when the incoming data changes

Note:

Not all properties in Vue are responsive, for example, $refs cannot monitor its changes;

The main difference in component construction is the

template generation method.

Template definition method

template option

String template

A string composed of HTML tag structure;

Example:

{
  template: &#39;<h1 id="简单示例">简单示例</h1>&#39;,
  props: {
    level: {
      type: Number,
      required: true
    }
  }
}
Copy after login

The template specified by the id selector

Wrapped with a script tag identified by id HTML fragment;

Example:

<script type="text/x-template" id="anchored-heading-template">
  <h1 v-if="level === 1">
    简单示例
  </h1>
</script>
{
  template: &#39;#anchored-heading-template&#39;,
  props: {
    level: {
      type: Number,
      required: true
    }
  }
}
Copy after login

render

Use the maximum programming capabilities of JavaScript. This function receives a createElement method as the first parameter Used to create VNode;

createElement receives three parameters: component root node type, component configuration object, child node (official description of component configuration object);

Example:

{
  render: function (createElement) {
    return createElement(
      &#39;h&#39; + this.level,   // tag name 标签名称
      this.$slots.default // 子组件中的阵列
    )
  },
  props: {
    level: {
      type: Number,
      required: true
    }
  }
}
Copy after login

vue single file component

Single file component structurally separates templates, logic, and styles and saves them in the same file.

<template>
  <div>
    ...
  </div>
</template>
<script>
...
export default{
  ...
}
...
</script>
<style>
...
</style>
Copy after login

Plan selection

templateSingle filerenderSimple structure of one lineConventional choicesChoices when the previous two solutions cannot solve the problem (high flexibility)

注意:不论选择哪一种方案,定义模板时,一定要有一个非template标签元素作根DOM,有且仅有一个。

vue组件注册方式

局部注册

以上几种方案定义的组件本质上都是一个对象,获取该对象(假设变量名为TabBar),要求只在另一个组件(假设变量名为App)内使用:

App组件的配置对象:

{
  components:{
    &#39;tab-bar&#39;: TabBar,
  }
}
Copy after login

这样就是局部注册,该组件TabBar只能在App模板中使用<tab-bar></tab-bar>,其它组件对TabBar不可见。

全局注册

以上几种方案定义的组件本质上都是一个对象,获取该对象(假设变量名为TabBar),要求项目内任何组件可使用:

一般在项目的入口文件(如:脚手架搭建项目的main.js)中:

Vue.component(&#39;tab-bar&#39;,TabBar);
Copy after login

这样就是全局注册,该组件TabBar能在整个项目内使用<tab-bar></tab-bar>,所有组件对TabBar可见。

vue组件生命周期钩子

以下用自己的语言将生命周期钩子表述一下:

What are vue components? Introduction to Vue components

beforeCreate

在这个时候,生命周期函数已经准备好。

组件实例已经构建,但本组件实例的数据、方法还没有注入;

可以在各个生命周期内通过组件实例this调用根实例上注入的$router$store等对象。

可以在本生命周期内进行数据初始化;

created

在这个时候,当前组件实例this上的属性($dataprops$methods...)已经注入绑定,可以调用本实例上的成员属性;

beforeMount

在进入本生命周期之前,会进行以下判断:

是否有el选项(指定挂载目标):

有el选项的是根实例;

没有el选项的是非根实例(默认挂载元素为组件调用的位置);

是否有template选项:

有template选项的是内联模板;

没有template选项的是单文件组件;

个人觉得,还有render选项的判断;

最终这些模板都会转换为render函数进行渲染!!!

这个生命周期在解析模板,不知道有什么实际用途。

mounted

在本生命周期之前,已经将模板渲染为真实DOM,其中vm.$el为组件实例的根DOM元素;

本生命周期是初始化第三方插件的场所;

必要时候,可以在本生命周期内对DOM进行操作;

本生命周期是获取this.$refs.ref的场所;

相关文章推荐:

Vue组件及数据传递详解

Vue组件的开发技巧总结

vue注册组件有几种方法

The above is the detailed content of What are vue components? Introduction to Vue components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles