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

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.

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

Display element or text on mouse hover in vue

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

Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following

What is the use of the immediate property of watch in Vue?

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

In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang

Setting up checkbox functionality in Vue

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

In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.

What happens if a child component changes the data in props in Vue?

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

In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t

How to refresh the page in Vue

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

Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial