-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
template syntax error <input :type="type" v-model="xxx">: v-model does not support dynamic input types. Use v-if branches instead. #3915
Comments
As the error suggested, v-model does not support dynamic input types, so don't use |
i am stuck in this error, someone can help me out ... thx |
ok ... thank you ... i should change the implement ... |
@yyx990803 Is this limitation going to be fixed? Or is there a workaround other than painful/unmaintenable v-if branches? |
@Akryum This is a feature introduced in this commit: f35f7e3. It seems that this check is skipped in production. Perhaps you can bypass this check by defining |
A better implementation would be to create your dynamic input type as a component. In Vue 2.x, you can attach |
@sirlancelot But in that component you would still need to dynamically bind the type, no? Or could the limitation be circumvented by using a custom model? |
@gwildu Yes, I encountered the same issue while trying to use an input component in (the development version of) Keen UI: https://josephuspaye.github.io/Keen-UI/#/ui-textbox-docs. After the recent update of Vue.js, now I cannot make it work. A workaround would be to always set the input type to |
@sirlancelot I'm not sure if this is possible but couldn't the old behavior be hidden by an attribute flag like 'generic-type' or 'dynamic-type'? My guess is, that this new behavior has a positive performance impact but that might not be an issue for all projects. |
How about implementing a render function in JavaScript? It does not look elegant but probably powerful enough to do almost anything. Actually, it is the way some components are implemented. https://vuejs.org/v2/guide/render-function.html |
Thanks for sharing that @akirak, I hadn't personally read that far in the docs yet but it outlines exactly what I had in mind for OP's dynamic input component which I suggested earlier: Create a component which has as many hard-coded inputs as desired and wrap them in a |
Render functions don't support v-model, so you'd have to re-implement v-model essentially, and that doesn't look that simple: https://github.com/vuejs/vue/blob/dev/src/platforms/web/compiler/directives/model.js |
The error doesn't seem to appear if you use |
You probably don't get all the functionality of v-model. Unless I'm mistaken, what's going to happen is that you'll get genDefaultModel here: https://github.com/vuejs/vue/blob/dev/src/platforms/web/compiler/directives/model.js#L27 So it won't work properly with select, checkbox and radio. |
@nkovacs The point for my use case is creating a generic text input component, part of a UI toolkit. It won't be used as a select/checkbox/radio (there will be specific components for that), but, it could be used as a text/email/password/phone/etc. text input. |
maybe I'm missing something but this works for me dynamic-input.vue
and then
or even
|
It does work in some cases, but not in others, e.g. if type is radio. v-model, when used on native tags (input, select) in vue template files, is compiled into different render functions depending on the tag and the type attribute. You can see that code here: https://github.com/vuejs/vue/blob/dev/src/platforms/web/compiler/directives/model.js Your code doesn't work on radio or checkboxes, for example, because it only listens to input events. v-model checks the type and listens to the correct event. |
ah okay, got it thanks :-) |
I worked around this by generating all the different input types I needed and then using v-if to activate only the one that matches the dynamic type (as suggested by the error message). I used pug to avoid a bunch of duplication:
This way I can use the built-in v-model directive. This can be extended to radio and checkboxes as well. |
interesting, thanks for sharing =) |
Here is my working solution in a component bootstrap based and also using vee-validate for this... Enjoy!
Another alternative and the way this framework push you to do it could be something like this...
|
<input ref="input" v-model="modelValue"> props: {
type: {
type: String,
default: 'text'
}
},
mounted () {
this.$refs.input.type = this.type
} |
interesting :D 👍 |
@meteorlxy Your solution will break when you use radio or checkbox. Vue 2.5.0 supports dynamic types by converting it to a v-if/v-else: f3fe012#diff-6eaa1698ef4f51c9112e2e5dd84fcde8R4, which looks similar to my workaround. It's buggy in 2.5.0, you have to call the property |
Why is this issue closed? Even if it isn't a bug, it seems reasonable that the API would support this? It works fine with <component :is="input" :type="type"> the only limitation is that it's weird and unreadable. |
Because it's implemented in 2.5.0: f3fe012#diff-6eaa1698ef4f51c9112e2e5dd84fcde8R4 |
V-model doesn't work with the |
Vue.js version
2.0.0-rc.1
Reproduction Link
Steps to reproduce
What is Expected?
What is actually happening?
The text was updated successfully, but these errors were encountered: