Introduction to the usage of props in Vue

Introduction to the usage of props in Vue

Preface:

In Vue, props can be used to connect originally isolated components in series, that is, child components can receive data passed by parent components. For example, if a child component wants to reference the data of the parent component, a variable can be declared in props, and this variable can reference the data of the parent element.

Example demonstration:

Subcomponents:

<template>

  <div>

    <h3>I am {{name}}, I am {{age}} years old, my hobby is {{hobby}}</h3>, {{flag}}

  </div>

</template>



<script>

export default {

    name:'Cpn',

    // Simple reception /* props:['age','hobby','name'], */

    // Declare the data to be received and restrict the received data when declaring props:{

        name: {

            //Declare type type:String,

            //Declare whether it is required: true,

            // Declare the default value default:'default value'

        },

        age:{

            type:Number,

            require:true,

            default:18

        },

        hobby:

            type:String,

            require:false

        },

        flag:{

            type:Boolean,

            require:false

        }

    }

}

</script>

Parent component:

<template>

  <div id="app">

    <!-- <cpn name='李明' age="22" hobby="playing ball"></cpn>

    <cpn name="Xiaohong" age="20" hobby="Playing the piano"></cpn> -->

    <cpn name='李明'></cpn>

    <cpn hobby="Programming" :flag="flag"></cpn>

      <!--Note: If you want to pass the data in the current component data to the child component, you need to pass it in the form of v-bing: variable name = "variable name". If the data passed is not in data, there is no need to add the binding instruction, that is, v-bind (can be abbreviated as:) -->

    <button @click="changeFlag">Switch</button>

  </div>

</template>

<script>

import Cpn from './components/Cpn.vue'

export default {

  components: { Cpn },

  name: "App",

  data() {

    return {

      flag:false

    }

  },

  methods: {

    changeFlag(){

      console.log(this.flag)

      this.flag=!this.flag;

      console.log(this.flag)

    }

  },

}

</script>

Running the above program, we can see that when we switch a value by clicking the button of the parent component, the value received by the child component will also change accordingly.

There are two ways for a child component to receive data from its parent component:

  • Method 1: Simple reception, just give the name of the variable to be received
  • Method 2: Specific reception, selectively specify the data type, whether it can be empty, and the default value for each received variable

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

You may also be interested in:
  • Solution to Vue subcomponent watch not listening to prop
  • How to use props to pass values ​​in Vue
  • A brief analysis of the details of props usage in Vue3 parent-child component value transfer
  • How to listen to props methods in Vue3.0

<<:  The implementation principle of Tomcat correcting the JDK native thread pool bug

>>:  CSS horizontal progress bar and vertical progress bar implementation code

Recommend

Native js to realize bouncing ball

On a whim, I wrote a case study of a small ball b...

jQuery implements employee management registration page

This article example shares the specific code of ...

Eight ways to implement communication in Vue

Table of contents 1. Component Communication 1. P...

CentOS installation mysql5.7 detailed tutorial

This article shares the detailed steps of install...

Detailed description of mysql replace into usage

The replace statement is generally similar to ins...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...

33 of the best free English fonts shared

ChunkFive Free Typefamily Cuprum JAH I Free font Y...

JavaScript to implement login form

This article example shares the specific code of ...

Vue uses OSS to upload pictures or attachments

Use OSS to upload pictures or attachments in vue ...

Let's talk about the v-on parameter problem in Vue

Use of v-on:clock in Vue I'm currently learni...

In-depth understanding of CSS @font-face performance optimization

This article mainly introduces common strategies ...

Summary of various methods for Vue to achieve dynamic styles

Table of contents 1. Ternary operator judgment 2....

A brief analysis of JS original value and reference value issues

Primitive values ​​-> primitive types Number S...

Vue3 AST parser-source code analysis

Table of contents 1. Generate AST abstract syntax...