JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Setting default values ​​for props in Vue

Author:JIYIK Last Updated:2025/02/27 Views:

Vue is a popular JavaScript framework that helps developers build interactive user interfaces. In Vue, components are the basic units for building user interfaces, and props are a common way to pass data between components. In this article, we will discuss how to set default values ​​for props in Vue.

1. Use default values

Props in Vue can set default values ​​to ensure that they work properly when no value is passed. To do this, you can use the default attribute in the component definition to specify the default value. For example, the following code demonstrates how to set a default value in a component:

Vue.component('my-component', {
  props: {
    message: {
      type: String,
      default: 'Hello World!'
    }
  },
  template: '<div>{{ message }}</div>'
})

In the code above, we define a component called my-component, which contains a prop called message. We specify the type of the prop as a string and set the default value to "Hello World!". In the component's template, we use the double bracket syntax to display the message value on the page.

If we do not pass the message property when using the component , the component will display the default value "Hello World!". For example:

<my-component></my-component>

2. Use functions

In some cases, we may need to dynamically calculate a default value based on the context of the component. In this case, we can use a function to set the default value. For example, the following code demonstrates how to use a function in a component to set a default value:

Vue.component('my-component', {
  props: {
    message: {
      type: String,
      default: function () {
        return 'Hello ' + this.name
      }
    },
    name: {
      type: String,
      default: 'World'
    }
  },
  template: '<div>{{ message }}</div>'
})

In the code above, we define a my-componentcomponent called with two props: message and name . We specify that the type of message is a string and use a function to calculate the default value. The function returns a string containing the value of the name property. If the name property is undefined, the default value "World" is used.

In the component's template, we use the double bracket syntax to display the message value on the page.

If we do not pass the message and name properties when using the component, the component will display the default value "Hello World!". For example:

<my-component></my-component>

If we pass a name attribute, the component will display the default value "Hello" plus the value of the name attribute. For example:

<my-component name="Vue"></my-component>

The component will display "Hello Vue".

3. Objects of use

In some cases, we may need to use an object to set the default value of props. For example, the following code demonstrates how to use an object to set the default value in a component:

Vue.component('my-component', {
  props: {
    user: {
      type: Object,
      default: function () {
        return {
          name: 'John Doe',
          age: 30
        }
      }
    }
  },
  template: '<div>{{ user.name }} is {{ user.age }} years old</div>'
})

In the code above, we defined a my-componentcomponent called with a prop called user. We specified the type of user as an object and used a function to set the default value. The function returns an object with two properties: name and age.

In the component's template, we use the double bracket syntax to display the property values ​​of the user object on the page.

If we don't pass the user property when using the component, the component will display the default value {name: 'John Doe', age: 30}. For example:

<my-component></my-component>

If we pass a user property, the component will display the passed property value. For example:

<my-component :user="{name: 'Jane Doe', age: 25}"></my-component>

The component will display "Jane Doe is 25 years old".


Summarize

Setting default values ​​for props in Vue is a useful technique to ensure that components still work properly when no value is passed. In this article, we discussed three ways to set default values ​​for props in Vue: using default values, using functions, and using objects. We also provided sample code to help you better understand how to set default values ​​for props in Vue.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Configuring Apache Web Server on Ubuntu and Debian

Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM

This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

My understanding of webservice is this

Publish Date:2025/03/18 Views:150 Category:NETWORK

Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat

Which technology do you choose to implement the web chat room?

Publish Date:2025/03/18 Views:62 Category:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

How to redirect a website from HTTP to HTTPS

Publish Date:2025/03/16 Views:117 Category:NETWORK

HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi

How to avoid soft 404 in the website during SEO

Publish Date:2025/03/17 Views:136 Category:NETWORK

This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand

Webpack packages ES6 and CommonJs mixed React

Publish Date:2025/03/02 Views:179 Category:React

This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial