Vue props that aren't explicitly declared
This is tricky and certainly not supported natively, however you can get the passed attributes from vnode
using this.$vnode.elm.attributes
and then loop through them and add them to a props object (which does need to be declared upfront). Because you will be processing this yourself I would put this in a mixin
so it can be reused, but you can do:
methods: { getProps() { let props = this.$vnode.elm.attributes; Object.keys(props).forEach(key => { this.$set(this.props, props[key].name, props[key].nodeValue) }); } }, data(){ return{ props: {} } }
With this you can then pass unknown props as if you declared them in your component.
I've made a JSFiddle to show you the process: https://jsfiddle.net/2c23Lb5t/
Here's how you would do this with a mixin
: https://jsfiddle.net/fej7wu5f/
You can use the $attrs
to get anything that isn't recognized as a prop: documentation. Note that the opposite also exists: you can get all the props with $props
.
Instead you can pass an object to child as props, and it can have all the properties you want.
It can be like:
<VfField :unknownPropObj=yourObj name="test" />
Where yourObj
can be: {"A": "abc", "B": "123"}
So you can define only on prop: unknownPropObj
in the child, and access all it's properties