DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • TDD Typescript NestJS API Layers with Jest Part 1: Controller Unit Test
  • API Analytics: Unleashing the Power of Data-Driven Insights for Enhanced API Management
  • Allow Users to Track Fitness Status in Your App
  • Data Distribution via API — Can a Single Developer Do It?

Trending

  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Mastering Advanced Aggregations in Spark SQL
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Unlocking AI Coding Assistants: Generate Unit Tests
  1. DZone
  2. Coding
  3. Languages
  4. How to Give Props Default Values in Vue

How to Give Props Default Values in Vue

Props are ubiquitous in Vue, so let's look at how to give them default values.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
May. 18, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.6K Views

Join the DZone community and get the full member experience.

Join For Free

When we use components in Vue, we often use properties or props to pass custom pieces of data down to the child component. For example, we can tell our child component that for this version of the component, "name" is set to "my-component":

Vue.js Component
 
<Component name="my-component" />

If we try to call this component without a name prop, it returns undefined in the code, or just as no text when rendered in HTML. Let's say our Component looks like this:

Vue.js Component
 
<script>
export default {
    props: {
        name: String
    },
    mounted() {
        console.log(this.name);
    }
}
</script>

<template>
    <p>
        Hi {{ name }}
    </p>
</template>

All our component does is defines a prop called name of type String, and console logs this property. It also displays it in the form Hi {{ name }}. The only issue here is that name is undefined when the component is called, no default name is given.

Setting Default Prop Values in Vue

Setting defaults prop values in Vue is easy. If you are using the Options API, then it's as easy as extending our property into an object. For example, if we want our name to have a default value of "there", then we update our prop to look like this:

Vue.js Component
 
export default {
    props: {
        name: {
            type: String,
            default: "there"
        }
    },
    mounted() {
        console.log(this.name);
    }
}

Now if no name is given, the message will simply say 'Hi there'

Setting Default Prop Values in Composition API

In the composition API, defining props uses the defineProps function. This function follows the same syntax as props defined on the Options API. Defining a prop without a default looks like this:

Vue.js Component
 
import { defineProps } from 'vue';

const props = defineProps({
    name: String
});

And then to add a default value, we extend name to have a default property, just as before:

Vue.js Component
 
import { defineProps } from 'vue';

const props = defineProps({
    name: {
        type: String,
        default: "there"
    }
});

Setting a Prop as Required in Vue

To avoid the need for setting a default value on a property, we can force a property to be required by using the required field. For example, if we want our name property to be defined, we'd simply set required to true:

Vue.js Component
 
<script setup>
import { defineProps } from 'vue';

const props = defineProps({
    name: {
        type: String,
        required: true
    }
});
</script>
API HTML IT Console (video game CLI) Data (computing) Object (computer science) Pass (software) Property (programming) Syntax (programming languages)

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • TDD Typescript NestJS API Layers with Jest Part 1: Controller Unit Test
  • API Analytics: Unleashing the Power of Data-Driven Insights for Enhanced API Management
  • Allow Users to Track Fitness Status in Your App
  • Data Distribution via API — Can a Single Developer Do It?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!