vue js default prop
In Vue.js, default props can be set for a component using the default
property in the props
object. Here are the steps to set a default prop in Vue.js:
Inside the component's options object, define the
props
property. This property should be an array or an object, depending on the desired prop types.Within the
props
array or object, define the props you want to set default values for. Each prop should be represented as a string in the array or as a key-value pair in the object.To set a default value for a prop, use the syntax
propName: { default: defaultValue }
. ReplacepropName
with the name of the prop you want to set a default value for, anddefaultValue
with the desired default value.Save the component file and run your Vue.js application. The component will now use the default value for the prop if no value is passed from its parent component.
Here's an example of setting a default prop in Vue.js:
<template>
<div>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
props: {
message: {
type: String,
default: 'Hello, world!' // Default value for the 'message' prop
}
}
}
</script>
With the above example, if the parent component doesn't pass a value for the message
prop, the child component will display "Hello, world!" as its default value.