JIYIK CN >

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

Setting default values for props in Vue

Author:JIYIK Last Updated:2025/04/20 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

Detailed explanation of Vue life cycle hooks

Publish Date:2025/04/20 Views:60 Category:Vue

Like other frameworks, Vue has a number of lifecycle hooks that allow us to attach code to specific events that occur when creating or using a Vue application — for example, when a component loads, when a component is added to the DOM, or

How to use MUI methods in Vue

Publish Date:2025/04/20 Views:141 Category:Vue

MUI is a front-end framework based on HTML5 and CSS3. It provides a series of UI components and JS plug-ins to help developers quickly build beautiful and smooth mobile web applications. Using MUI in Vue can further improve development effi

Implementing paging in Vue

Publish Date:2025/04/20 Views:196 Category:Vue

Pagination is one of the common features in modern web applications. It allows users to browse large amounts of data by dividing the data into multiple pages. In Vue, we can use some libraries and components to implement pagination. In this

Setting up a countdown timer in Vue

Publish Date:2025/04/20 Views:194 Category:Vue

Countdown timers are a very common feature in modern web applications. In Vue, we can easily implement a countdown timer to provide feedback to users when they perform certain actions. In this article, we'll look at how to set up a countdow

Import JSON file into Vue

Publish Date:2025/04/20 Views:188 Category:Vue

Vue.js is a popular JavaScript framework that can quickly build single-page applications (SPA). In Vue.js, we can use JSON files to store data, which is very useful in development. This article will introduce how to introduce JSON files in

Different ways to send a POST request in Vue

Publish Date:2025/04/20 Views:151 Category:Vue

In Vue, sending a POST request is a very common operation, which can be achieved in many ways. This article will introduce several methods of sending a POST request in Vue, and will explain it in detail with examples. Method 1: Using Vue-re

Expand and collapse text in Vue

Publish Date:2025/04/20 Views:114 Category:Vue

With the rapid development of mobile Internet, users have higher and higher requirements for page interactivity and user experience. The functions of expanding and collapsing text are becoming more and more common in page design, which can

Dynamically add properties and values to objects in Vue

Publish Date:2025/04/20 Views:199 Category:Vue

In Vue, we often need to manipulate the properties and values ​​of objects. Sometimes we need to dynamically add properties and values ​​to meet business needs. This article will introduce in detail how to dynamically add properties

How to scroll to the top or bottom of the page in Vue.js

Publish Date:2025/03/01 Views:96 Category:Vue

Vue.js is a popular front-end framework that helps developers build efficient and maintainable applications. In Vue.js, scrolling the page to the top or bottom is a common requirement. In this article, we will introduce how to implement this

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial