-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Using v-model on a custom Element (from defineCustomElement): prop/event mismatch #4428
Comments
Strictly speaking this is not really a bug as the custom element does what it's supposed to do: it emits the component event as a custom event. The problem is that v-model listens for However we should discuss how we can better support this common usecase. |
On second thought, I'm inclined to rate this as a bug. There's a few different aspects to this, the thing to discuss is wethe we want to support v-model on a custom element at all. We currently support it on distinct native input elements only (and components, but we are dealing with a custom DOM element here). If we decided to do this, we would have to make the v-model listener deal with custom events, where the emitted value is found in a But regardless of that discussion, the thing that makes it a bug right now, though, is that there's no way to actually listen to the So even if the consumer doesn't use v-model and tries the following, they will fail: <custom-element :model-value="value" @update:model-value="value = $event.detail" ...because the event listener is never being applied to the custom element. Which is desirable for normal DOM elements, of course, but poses an issue here. I'll think a bit about it and write more thoughts later |
The styles of child vue component are not applied if the parent component is a custom element. #4309 I thank you for your help and hope that a solution/workaround will be found soon. 🙂 ✌ue is awesome! |
I didn't want to create another issue for it, as this seems related: v-model does not work on For Bumped into this as my use case for this was single component for both textarea and input. |
@LinusBorg Did you think about it? It's November 2023 and this is still an issue. It's also a regression from Vue 2.x (https://www.digitalocean.com/community/tutorials/how-to-add-v-model-support-to-custom-vue-js-components) and is blocking me personally from achieving my goal of creating custom elements that do support v-model (like a custom for example). |
I have a PR up that addresses this issue (at least partially) for custom elements. It allows you to specify a |
First of all, a general opinion: Vue custom elements are first and foremost custom elements, so they should strive to behave like native DOM elements where applicable. It is discouraged to author Vue custom elements that only work in Vue applications. With that in mind, a Vue custom input element should behave like a native When Therefore, to make a Vue custom element work with
Before 3.5, retrieving the host element can be a bit difficult. That's why 3.5 will ship with a new Here's a working example using the |
Thanks for adding I'd be happy to raise a PR to mention this in the Vue docs, if so! There is a bit more detail in this feature discussion around why I think this would be a helpful thing to document or have an official solution for, vuejs/rfcs#617: Setting The workaround here is to set a <template>
<custom-checkbox type="checkbox" v-model="value">Label</custom-checkbox>
</template>
<script>
import "@custom-element-package/checkbox.js"
export default {
data() {
return {
value: true
};
},
};
</script> |
Version
3.2.4
Reproduction link
CodeSandbox Link
Steps to reproduce
Create a custom input as a CustomElement and use it in a Vue component.
What is expected?
The value specified in the v-model changes
What is actually happening?
v-model is ignored.
The text was updated successfully, but these errors were encountered: