Basic usage details of Vue componentization

Basic usage details of Vue componentization

Preface:

Sometimes there is a set of HTML structured code, and events may be bound to it. Then this code may be used in multiple places. If it is copied here and there, a lot of code will be repeated, including the code in the event part. Then at this time we can encapsulate these codes into a component, and when we use it in the future, we can just use it like using ordinary html elements.

1. What is componentization?

vue.js has two magic weapons, one is data-driven, the other is componentization, so the question is, what is componentization and why do we need componentization? Next, I will answer these two questions one by one. The so-called componentization is to split the page into multiple components, and develop and maintain CSS , JS , templates, pictures and other resources that each component depends on together. Because components are resource independent, they can be reused within the system and can be nested. If the project is complex, the amount of code can be greatly simplified and it is also more friendly to later demand changes and maintenance.

2. Basic use

<div id="app">
  <button-counter></button-counter>
  <button-counter></button-counter>
  <button-counter></button-counter>
</div>
<script>
  // Define a new component called button-counter Vue.component('ButtonCounter', {
    data: function () {
      return {
        count: 0
      }
    },
    template: '<button @click="count++">Clicked {{ count }} times</button>'
  })
  const app = new Vue({
    el: "#app",
    data: {
      message: "hello"
    }
  })
</script>

Above we created a component called button-counter , which implements the function of recording how many times a button is clicked. If we want to use it later, we can just use it through button-counter . Then because components are reusable Vue instances, they receive the same options as new Vue , such as data , computed , watch , methods , and lifecycle hooks. The only exceptions are root instance-specific options like el.

Another thing to note is that data in the component must be a function!

Let's take a look at the results achieved:

We used the button-counter component three times above, so the page will show three of them, and each component will maintain its count independently, because every time you use a component, a new instance of it will be created. Each instance can maintain an independent copy of the returned object, which is why we use the function in data

This is the end of this article about the basic usage details of Vue componentization. For more relevant Vue componentization usage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Do you know the componentization in Vue life cycle?
  • Vue component learning scoped detailed explanation
  • Essential skills for Vue component development: component recursion
  • Detailed explanation of component development of Vue drop-down menu
  • Common methods of Vue componentization: component value transfer and communication
  • Let's learn Vue's componentization together

<<:  MySQL string splitting operation (string interception containing separators)

>>:  5 Simple XHTML Web Forms for Web Design

Recommend

mysql8.0.11 winx64 installation and configuration tutorial

The installation tutorial of mysql 8.0.11 winx64 ...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

Example of compiling LNMP in Docker container

Table of contents 1. Project Description 2. Nginx...

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

Introduction to the common API usage of Vue3

Table of contents Changes in the life cycle react...

Mysql table creation foreign key error solution

Database Table A: CREATE TABLE task_desc_tab ( id...

Tutorial on installing mysql under centos7

Recently, I plan to deploy a cloud disk on my hom...

In-depth understanding of Mysql logical architecture

MySQL is now the database used by most companies ...

MySQL database connection exception summary (worth collecting)

I found a strange problem when deploying the proj...

About installing python3.8 image in docker

Docker Hub official website 1. Search for Python ...

Implementing a simple Gobang game with native JavaScript

This article shares the specific code for impleme...

Graphic tutorial on installing Ubuntu 18.04 on VMware 15 virtual machine

In the past few years, I have been moving back an...

jQuery implements employee management registration page

This article example shares the specific code of ...