I have a component set like:
export default {
props: {
prop1: {
type: String,
required: true,
},
}
}
If I don't provide the required prop1
, Vue emits a warning like
[Vue warn]: Missing required prop: "prop1"
I'm wondering, how can I catch this warn and do something about it?
My use case is using the component as a page, with props: true
on the VueRouter.
If it's a navigation inside my application using $router.push({params: {prop1: 'whatever' }})
, everything works well.
But if the user lands on the url, the props won't be set and I want to display a message or redirect him or whatever.
I know I can make the props not required and validate it on the mounted
, but it seems duplicating code.
I'm imagining something like Component.mountedError()
required
flag and propvalidator()
are intended for development only, so they're not included in the production/minified build, and you wouldn't be able to catch any errors from them. The behavior you're seeking indeed requires custom validation inmounted()
. - tony19