Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Access HTMLAttributes using script setup

So I have an Input component, and I would like to inherit all the default Input Attributes, this is my script

<script setup lang="ts">
import { defineProps, InputHTMLAttributes } from "vue";

interface Props extends InputHTMLAttributes {
  label?: string;
}

defineProps<Props>();
</script>

I would like to know what am I supposed to put in my tag to get the attributes. This is my template so far:

<template>
  <div>
    <span v-show="label">{label}</span>
    <input {?????} />
  </div>
</template>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add another script tag in which make inheritAttrs:false then bind $attrs to the input tag :

<script setup lang="ts">
import { defineProps, InputHTMLAttributes } from "vue";

interface Props extends InputHTMLAttributes {
  label?: string;
}

defineProps<Props>();
</script>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  inheritAttrs:false
})
<template>
  <div>
    <span v-show="label">{label}</span>
    <input v-bind="$attrs" />
  </div>
</template>
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading