Introduction to local components in Vue

Introduction to local components in Vue

In Vue, we can define (register) local components ourselves

To define a component name:

var ComponentA = { /* ... */ }

var ComponentB = { /* ... */ }

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

new Vue({
  el: '#app',
 
// Component center components: {
 // When rendering a local registered component at the view layer // component-a: the name you want to use when calling at the view layer // ComponentA: Local registered component name 'component-a': ComponentA,
    'component-b': ComponentB
  }
})


Call the local component in the view layer:

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


For example:

<body>
    
    <div id="app">
        <component-a></component-a>
        <component-b></component-b>
    </div>
 
    <script src="./js/vue.js"></script>
    <script>
        let componentA = {
            template:`
                <p>I am a local component 1</p>
            `
        }
 
        let componentB = {
            template:`
                <p>I am a local component 2</p>
            `
        }
 
        let vm = new Vue({
            el:'#app',
            data:{
 
            },
            components:{
                "component-a":componentA,
                "component-b":componentB
            }
        })
    </script>


The output is:

You may also be interested in:
  • Vue local component data sharing Vue.observable() usage
  • Description of the difference between Vue global components and local components
  • VUE registration global components and local components process analysis
  • Vue component definition, global and local components, template and dynamic component function examples
  • In-depth analysis of the difference between Vue global components and local components
  • Detailed explanation of how to use Vue global components and local components
  • Detailed explanation of the use of global components and local components in Vue components
  • Detailed explanation of vue.js global components and local components

<<:  Perfect solution to Google Chrome autofill problem

>>:  Textarea tag in HTML

Recommend

Why are the pictures on mobile web apps not clear and very blurry?

Why? The simplest way to put it is that pixels are...

JS canvas realizes the functions of drawing board and signature board

This article shares the specific code of JS canva...

How to implement parent-child component communication with Vue

Table of contents 1. Relationship between parent ...

A brief discussion on MySQL large table optimization solution

background The amount of new data in the business...

CSS3 speeds up and delays transitions

1. Use the speed control function to control the ...

IE6 implements min-width

First of all, we know that this effect should be ...

How to automatically back up the mysql database regularly

We all know that data is priceless. If we don’t b...

CSS navigation bar menu with small triangle implementation code

Many web pages have small triangles in their navi...

The whole process of installing and configuring Harbor1.7 on CentOS7.5

1. Download the required packages wget -P /usr/lo...

Vue uses canvas to realize image compression upload

This article shares the specific code of Vue usin...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arr...

CSS to achieve the small sharp corner effect of bubbles

Effect picture (the border color is too light, pu...