Detailed explanation of Vue components

Detailed explanation of Vue components

insert image description here

insert image description here

insert image description here

<body>
    <div id="root">
        <h2>{{name}}</h2>
        <hr>
        <school></school>
        <hr>
        <student></student>
        <hr>
        <!-- <h2>Student name: {{name}}</h2>
        <h2>Student age: {{age}}</h2> -->
    </div>
    <div id="root2">
        <hello></hello>
    </div>
    <script>
        Vue.config.productionTip = false;
        //Create school component//el:'#root'
        //When defining a component, be sure not to write the el configuration item, because ultimately all components will be managed by a vm, which will decide which container to serve const school = Vue.extend({
            template: `
            <div>
                <h2>School name: {{schoolName}}</h2>
                <h2>School address: {{address}}</h2>
               <button @click="showName">Click me to show the school name</button>
                </div>
           `,
            data() {
                return {
                    schoolName: 'Second Middle School',
                    address: 'Beijing',
                }
            },
            methods: {
                showName() {
                    alert(this.schoolName)
                }
            }
        })
        //The first step: create components //Create student components const student = Vue.extend({
            template: `
         <div>  
        <h2>Student name: {{name}}</h2>
        <h2>Student age: {{age}}</h2>
        </div>
           `,
            data() {
                return {
                    name: 'Xiao Wang',
                    age: 20,
                }
            }
        })
        //Create vm
        new Vue({
            el: '#root',
            data: {
                name: 'Hello, World! '
            },
            //Step 2: Register components (local registration)
            components:
                school,
                student
            }
        })

        const hello = Vue.extend({
            template: `
            <div><h2>Hello! Student Wang</h2></div>
            `
        })
        Vue.component('hello', hello)
        new Vue({
            el: '#root2'
        })
    </script>
</body>

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of custom events of Vue components
  • Detailed explanation of value transfer between parent and child components in Vue3
  • Detailed explanation of various methods of Vue component communication
  • Vue components dynamic components detailed explanation
  • Detailed explanation of the use of Vue image drag and drop zoom component
  • Detailed explanation of the use of router-view components in Vue

<<:  Simple example of using Docker container

>>:  CSS hacks \9 and \0 may not work for hacking IE11\IE9\IE8

Recommend

Build nginx virtual host based on domain name, port and IP

There are three types of virtual hosts supported ...

Detailed explanation of the use of MySQL group links

Grouping and linking in MYSQL are the two most co...

Tutorial on binary compilation and installation of MySql centos7 under Linux

// It took me a whole afternoon to install this, ...

Solution to garbled display of Linux SecureCRT

Let's take a look at the situation where Secu...

JS, CSS style reference writing

CSS: 1. <link type="text/css" href=&q...

How to Install Oracle Java 14 on Ubuntu Linux

Recently, Oracle announced the public availabilit...

mysql data insert, update and delete details

Table of contents 1. Insert 2. Update 3. Delete 1...

The difference and usage of LocalStorage and SessionStorage in vue

Table of contents What is LocalStorage What is Se...

MySQL 8.0.22 winx64 installation and configuration graphic tutorial

mysql 8.0.22 winx64 installation and configuratio...

HTML Several Special Dividing Line Effects

1. Basic lines 2. Special effects (the effects ar...

Getting Started with Vue 3.0 Custom Directives

Table of contents 1. Custom instructions 1. Regis...

JavaScript to implement voice queuing system

Table of contents introduce Key Features Effect d...