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

Explore how an LED can get you started with the Linux kernel

Table of contents Preface LED Trigger Start explo...

Tutorial on using Webpack in JavaScript

Table of contents 0. What is Webpack 1. Use of We...

MySQL InnoDB transaction lock source code analysis

Table of contents 1. Lock and Latch 2. Repeatable...

Website construction experience summary

<br />What principles should be followed to ...

MySQL master-slave replication delay causes and solutions

Table of contents A brief overview of the replica...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...

Implementation principle and configuration of MySql master-slave replication

Database read-write separation is an essential an...

Detailed explanation of Vuex overall case

Table of contents 1. Introduction 2. Advantages 3...

React Fiber structure creation steps

Table of contents React Fiber Creation 1. Before ...

Creating private members in JavaScript

Table of contents 1. Use closures 2. Use ES6 clas...

How to make JavaScript sleep or wait

Table of contents Overview Checking setTimeout() ...