I'm trying to extract some repeated code into a Vue component. I want to be able to pass a list of CSS classes into the component using the class HTML attribute, and merge those with a "default class" that is defined in the component. I want the public interface of this component to resemble a standard HTML element, but if I try to use "class" as a prop Vue throws an error about using a JS keyword. If I try to use $attrs, my default class gets wiped out.
Is it possible to merge HTML attributes with local component data, and use the result in my template? Below is what I would like to achieve:
<template>
<img src="imageUrl" class="classes"
</template>
export default {
computed: {
imageUrl() { return 'urlString' },
},
classes() { return `${this.$attrs.class} default-class` }
}
And I'd expect an implementer to be able to use my component like so:
<CustomImageComponent class="class-name another-class" />
Which I'd expect to render this:
<img src="urlString" class="class-name another-class default-class" />
class="my-static-class" :class="my-passed-classes". which combines static and variables in class name