How to get v-model expression?

I need expression passed in v-model to detect property name.

Example:

<input-component v-model="attributes.name" />

Why do I need expression?
If the expression attributes.name is available then input components can fetch relevant errors from injected error collection object.

Until v2.1, we could get expression using this.$vnode.data.directives.model.

1 Like

Seems this isn’t possible, see my re-post of the question here:

Check mounted method in https://github.com/znck/bootstrap-for-vue/blob/master/src/mixins/inputHelper.js

This is not part of public API and can change anytime.

v-model is merely syntax sugar (https://vuejs.org/v2/guide/components.html#Form-Input-Components-using-Custom-Events) so perhaps you can find your solution by looking at v-bind https://vuejs.org/v2/api/#v-bind

<input
  v-bind:value="something"
  v-on:input="something = $event.target.value">

If you find nothing there, the first thing that comes to mind could be using a prop to assist you in matching your keys.

znck’s answer seems to work well – this.$vnode.data.model.expression

I’ll ask if this can be brought into the public API given that it’s already there and useful for frameworks.

1 Like

Note: for v-model="field_name"
In vue2: node.$vnode.data.model.expression
In vue3: node.$attrs['onUpdate:modelValue'].toString() returns $event => ((_ctx.field_name) = $event), so you can get your field_name from there.
(but it’s not public api…)