Hi @TimotheeWright
I have also tried it using props,
if you try to use mounted hook to change the styling according to the component, the mounted is not called for other instance of the component, so inline styling also works. we can use v-bind in style and use the prop.
App.vue
<template>
<HelloWorld /><br />
<HelloWorld color="yellow" /><br />
<HelloWorld color="green" />
</template>
<script>
import HelloWorldVue from "./components/HelloWorld.vue";
export default {
name: "App",
components: {
HelloWorld: HelloWorldVue,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
helloworld.vue
<template>
<div>
<button id="btn" :style="{ 'background-color': color }">
{{ color }}
</button>
</div>
</template>
<script>
export default {
name: "HelloWorld",
props: {
color: {
type: String,
default: "red",
},
},
data() {
return {
ccolor: [1, 2, 3, 4],
};
},
methods: {
chngArray() {
console.log(this.ccolor);
},
},
// mounted() {
// document.getElementById("btn").style.backgroundColor = this.color;
// },
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
This is an alternative solution by using the props.
src code link: clever-violet-8s24of - CodeSandbox