message to the child, what will the child render?<template> <ChildComponent :message="parentMessage" /> </template> <script setup> import ChildComponent from './ChildComponent.vue' import { ref } from 'vue' const parentMessage = ref('Hello from parent') </script> <!-- ChildComponent.vue --> <template> <p>{{ message }}</p> </template> <script setup> const props = defineProps({ message: String }) </script>
The parent passes parentMessage as a prop named message. The child receives it via defineProps and renders it inside a paragraph. So the output is the string value.
title that must be a string and is required. Which code snippet is correct?In Vue 3, to define a required prop with a type, you use an object with type and required keys. The type must be the constructor (e.g., String), not a string.
<template> <ChildComponent :count="count" /> <button @click="count++">Increment</button> </template> <script setup> import { ref } from 'vue' import ChildComponent from './ChildComponent.vue' const count = ref(0) </script> <!-- ChildComponent.vue --> <template> <p>Count is: {{ count }}</p> </template> <script setup> const props = defineProps({ count: Number }) </script>
Props in Vue are reactive. When the parent updates count, the child receives the new value and re-renders accordingly.
userName as a prop, but the child shows an empty string. What is the problem?<!-- Parent.vue --> <template> <ChildComponent user-name="Alice" /> </template> <script setup> import ChildComponent from './ChildComponent.vue' </script> <!-- ChildComponent.vue --> <template> <p>User: {{ userName }}</p> </template> <script setup> const props = defineProps({ userName: String }) </script>
Without the colon, user-name="Alice" passes the literal string 'Alice' as a string attribute, but Vue treats it as a plain HTML attribute, not a prop. Using :userName="'Alice'" binds the string as a prop properly.
<template>
<p>{{ count }}</p>
<button @click="count++">Increase</button>
</template>
<script setup>
const props = defineProps({ count: Number })
</script>Vue treats props as read-only inside child components. Mutating them directly triggers a warning and does not change the prop value.