We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
This is the default way to specify props and emits in VueJS:
<script setup lang="ts">
// This is called "runtime declaration"
const props = defineProps({
modelValue: {
type: Object as PropType<User>,
required: false,
},
disabled: {
type: Boolean,
default: false,
},
placeholder: {
type: String,
default: '',
},
});
const emit = defineEmits(['update:modelValue', 'close', 'change', 'input']);
</script>
If you are using TypeScript, the better way to define them is:
<script setup lang="ts">
interface Props {
modelValue?: User;
disabled?: boolean;
placeholder?: string;
}
// Here, we use pure types via a generic type argument
// Also called type-based declaration
const props = withDefaults(defineProps<Props>(), {
modelValue: null,
disabled: false,
placeholder: '',
});
const emit = defineEmits<{
'update:modelValue': [User];
close: [];
change: [number];
input: [User];
}>();
</script>
The advantage of doing it like this is that you add type-safety in the both the props and the emits.
You can find more info about this in the VueJS documentation.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.