简体   繁体   中英

Vue styling components

I have a basic vue component. It looks like this:

 <script> export default { props: { source: { type: String, required: true, } }, } </script>
 <template> <div class="imageItem"> <img class="image" :data-srcset="source" /> <p> this is some text </p> </div> </template>

What I struggle to achieve is, when I load the component in the parent that looks something like this:

<imageItem :source="source" />

I want to be able to customize the component styles. For example something like this:

<imageItem :source="source" :textColor="red" />

I'm guessing I should do it with props also but its not working as expected.

Can someone share a proper way to do this ?

Try this:

 <script>
export default {
    props: {
        source: {
            type: String,
            required: true,
        },
        style:{
            type: String,
            required:true
        }            
    },
}
</script>

<template>
  <div class="imageItem">
    <img class="image" :data-srcset="source" :style="style" />
    <p> this is some text </p>
 </div>
</template>

And then in the prop: "color:red"

<imageItem :source="source" :style="color : red" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM