I want to add padding to the :style directive of my shared button component but for some reasons, the changes aren't showing up on the button.
I tried doing it with 3 ways below to apply the changes but none of them worked. I'm new to Vuejs and not able to find the problem. Any inputs/suggestions would be appreciated.
<Button
@on-click="currentStep = 2"
:text= "Next"
:style="'padding: 12px 15px 12px 15px'"
/>
<Button
@on-click="currentStep = 2"
:text= "Next"
:style="{ padding: '12px 15px 12px 15px' }"
/>
<Button
@on-click="currentStep = 2"
:text= "Next"
:style="myStyle"
/>
and then returning myStyle: { padding: '12px 15px 12px 15px' } in the script for third one.
My Button.vue component looks like this:
<template>
{{ this.color }}
<button
@click="onClick"
:disabled="disabled"
class="begin-btn"
:style="backgroundColor + textColor"
>{{ text }}
</button>
</template>
<script>
export default {
name: 'ButtonComponent',
props: {
text: String,
disabled: Boolean,
width: String,
bgColor: String,
txtColor: String,
},
data() {
return {}
},
computed: {
backgroundColor(){
let bgColor = this.bgColor ? this.bgColor : '#d64ba1'
return "background: " + bgColor + ';';
},
textColor(){
let textColor = this.txtColor ? this.txtColor : '#ffffff'
return "color: " + textColor + ';';
}
},
methods: {
onClick() {
this.$emit("onClick");
},
},
}
</script>
<style scoped>
.begin-btn {
justify-content: center;
align-items: center;
border: 0px;
width: 100%;
height: 44px;
background: #d64ba1;
border-radius: 24px;
font-style: normal;
font-weight: 700;
font-size: 16px;
color: #ffffff;
}
</style>
You can use same approach as with another properties: define a property for padding and set a default value for in in your component's computed.
Then, override the padding value with the one you need in another template where you use your component:
SomeView.vue
UPDATE: You don't even need computed for this. Following code also works fine:
And then pass the required value to component: