Vue component organization structure and component registration details

Vue component organization structure and component registration details

1. Component Organization

Typically an application is organized as a nested component tree:

For example, we may have components such as headers, sidebars, and content areas, each of which may contain other components such as navigation links and blog posts.

In order to be used in templates, these components must first be registered so that Vue can recognize them. There are two types of component registration: global registration and local registration.

So far, our components are all registered globally through Vue.component :

Vue.component('my-component-name', {
  // ... options ...
})


Globally registered components can be used in any newly created Vue root instance (via new Vue ) after they are registered, including the templates of all child components in their component tree.

2. Component name

When registering a component, we always need to give it a name. For example, we have seen this when registering globally:

Vue.component('my-component-name', { /* ... */ })


The component name is the first parameter of Vue.component

2.1 Component Naming

There are two ways to define component names:

  • Hyphen-separated names: my-component-name
  • Capitalize the first letter of the name: MyComponentName

Hyphen separated names

Vue.component('my-component-name', { /* ... */ })

When defining a component using (hyphen separated names), for example: <my-component-name></my-component-name>

Capitalize the first letter

Vue.component('MyComponentName', { /* ... */ })

When defining a component using (capitalized first letter), you can use either naming convention when referencing the custom element. That is, both <my-component-name> and <MyComponentName> are acceptable.

Note: However, only hyphen-delimited names are valid when used directly in the DOM (i.e. non-string templates).

3. Global Registration

Global registration is to use Vue.component to create components:

Java
Vue.component('my-component-name', {
  // ... options...
})

These components are registered globally. This means that they can be used in the template of any newly created Vue root instance ( new Vue ) after registration.

for example:

<div id="app">
  <component-a></component-a>
  <component-b></component-b>
  <component-c></component-c>
</div>

Vue.component('component-a', { /* ... */ })
Vue.component('component-b', { /* ... */ })
Vue.component('component-c', { /* ... */ })

new Vue({ el: '#app' })

However, global registration is not often used in actual projects.

4. Partial Registration

Global registration is often suboptimal. For example, if you use a build system like webpack , registering all components globally means that even if you no longer use a component, it will still be included in your final build. This results in an unnecessary increase in the amount of JavaScript that users have to download.

In these cases, you can define the component via a plain JavaScript object:

let ComponentA = {
    template: `<p>hello</p>`
  }
let ComponentB = {
  template: `<p>world</p>`
}


Then define the components you want to use in the components option:

new Vue({
  el: '#app',
  components:
    'component-a': ComponentA,
    'component-b': ComponentB
  }
})

For each property in the components object, the property name is the name of the custom element, and the property value is the options object for this component.

Of course, in the actual development process, we use the module system to register more components, which will be introduced later.

This is the end of this article about the organizational structure of Vue components and the details of component registration. For more relevant Vue component organizational structure and component registration 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:
  • How to implement tab components in Vue.js
  • How to use icon component in Vue.JS
  • Detailed explanation of four ways to customize Vue components
  • How to use AIlabel to label components in Vue

<<:  MySQL string splitting example (string extraction without separator)

>>:  Docker+selenium method to realize automatic health reporting

Recommend

Detailed explanation of MySQL replication principles and practical applications

This article uses examples to illustrate the prin...

Native JS to achieve sliding button effect

The specific code of the sliding button made with...

MySQL Order By Multi-Field Sorting Rules Code Example

Say it in advance On a whim, I want to know what ...

Bootstrap 3.0 study notes grid system principle

Through the brief introduction in the previous tw...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

Example code for implementing div concave corner style with css

In normal development, we usually use convex roun...

JavaScript array deduplication solution

Table of contents Method 1: set: It is not a data...

CSS layout tutorial: How to achieve vertical centering

Preface I have been summarizing my front-end know...

Detailed explanation of the use of find_in_set() function in MySQL

First, let’s take an example: There is a type fie...

How to uninstall MySQL 5.7 on CentOS7

Check what is installed in mysql rpm -qa | grep -...

isPrototypeOf Function in JavaScript

Table of contents 1. isPrototypeOf() Example 1, O...

Several ways to use require/import keywords to import local images in v-for loop

Table of contents Problem Description Method 1 (b...

Detailed explanation of the platform bus of Linux driver

Table of contents 1. Introduction to platform bus...