Reputation: 81
I made a custom element extends built-in element like <button is="my-button">
. But it's not working when I use it in VueJs. Because maybe VueJs use is
attribute for its components.
Is there any good solution for this?
<button is="my-button">
-> <my-button>
:
Vue try to make its component. and then my button is broken.
<button is="my-button" v-pre>
:
It works for button
element but data binding in button is not working.
<button v-my-custom-directive="'my-button'">
:
It works and it's my latest solution... But it seems ugly. I should use quotation marks for avoid undefined variable error
. I don't want to suggest other people who use my element in VueJs.
Sample code: https://jsbin.com/yucojat/2/edit?html,js,output
Upvotes: 3
Views: 175
Reputation: 56783
In Vue3, which still doesn't support customized built-ins in standard Vue templates (which strip the is="my-button"
attribute), you can use the h()
function, which supports dynamic creation of CBIs.
Example:
<script setup>
import { h, defineComponent } from 'vue';
export default defineComponent({
render() {
return h('button', { is: 'my-button' }, 'Hello, Vue!');
}
});
</script>
Unfortunately, when using the component, anything inside of the component will work as if you had a v-once
directive on the component, that is, initially a binding will work, but with no reactivity. Could still be useful for exactly those cases where you would want to use v-once
anyway.
At the end of the day, this appears to be an awful workaround for a limitation that comes with just about all popular frameworks, not just Vue. E.g. JSX doesn't support is
at all, which basically means you cannot use CBIs in React, Svelte doesn't support is
, etc.
Upvotes: 0