javascript / intermediate
Snippet
Runtime Validation for Custom Events
You can validate the arguments passed to custom events by providing a validator function in defineEmits. If it returns false, Vue will output a warning in development, improving code reliability.
snippet.js
1
2
3
4
5
const emit = defineEmits({submit: ({ email, password }) => {return email.includes('@') && password.length > 8;}});
vue
Breakdown
1
submit: ({ email, password }) => {
Define a key matching the event name and a validation callback.
2
return email.includes('@') && ...
Check if the payload meets specific criteria and return a boolean.