<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AbdulKareem Alabi</title>
    <description>The latest articles on DEV Community by AbdulKareem Alabi (@designbyalabi).</description>
    <link>https://dev.to/designbyalabi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F865546%2F9755947d-cbc8-40d0-a2f4-8509a6d537c6.jpeg</url>
      <title>DEV Community: AbdulKareem Alabi</title>
      <link>https://dev.to/designbyalabi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/designbyalabi"/>
    <language>en</language>
    <item>
      <title>How to style a text using an object and computed property in vue.js</title>
      <dc:creator>AbdulKareem Alabi</dc:creator>
      <pubDate>Sat, 27 Jan 2024 18:47:52 +0000</pubDate>
      <link>https://dev.to/designbyalabi/how-to-style-a-text-using-an-object-and-computed-property-in-vuejs-37gk</link>
      <guid>https://dev.to/designbyalabi/how-to-style-a-text-using-an-object-and-computed-property-in-vuejs-37gk</guid>
      <description>&lt;p&gt;styling in vue.js can be done using many ways, but how to use object for class binding seems a bit challenging because all the value in the object get applied at once, here is an example of what i mean 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
 &amp;lt;label
      &amp;gt;&amp;lt;input
        type="radio"
        name="rating"
        value="Bad"
        v-model="rating"
      /&amp;gt;
      Bad&amp;lt;/label
    &amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label
      &amp;gt;&amp;lt;input
        type="radio"
        name="rating"
        value="Okay"
        v-model="rating"
      /&amp;gt;
      Okay&amp;lt;/label
    &amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label
      &amp;gt;&amp;lt;input
        type="radio"
        name="rating"
        value="Good"
        v-model="rating"
      /&amp;gt;
      Good&amp;lt;/label
    &amp;gt;
&amp;lt;p :class="{
        bad: 'rating',
        okay: 'rating',
        good: 'rating',}"
&amp;gt;Rate this page: {{ rating }} &amp;lt;/p&amp;gt;

&amp;lt;/template&amp;gt;


&amp;lt;script&amp;gt;
export default {
  data() {
    return {
      rating: "",
    };
  },
};
&amp;lt;/script&amp;gt;

&amp;lt;style&amp;gt;
.bad {
  color: red;
}

.okay {
  color: purple;
}

.good {
  color: green;
}
&amp;lt;/style&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the method above will work, but the last value which is green styling will only be applied, but if you inspect in the browser you will see all the object properties was applied, that leaves us with wondering while is other styling not applying when we switch to other rating, so how to fix it is to use computed properties 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;p :class="activeColor"&amp;gt;Rate this page: {{ rating }}&amp;lt;/p&amp;gt;

    &amp;lt;label
      &amp;gt;&amp;lt;input
        type="radio"
        name="rating"
        value="Bad"
        v-model="rating"
      /&amp;gt;
      Bad&amp;lt;/label
    &amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label
      &amp;gt;&amp;lt;input
        type="radio"
        name="rating"
        value="Okay"
        v-model="rating"
      /&amp;gt;
      Okay&amp;lt;/label
    &amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label
      &amp;gt;&amp;lt;input
        type="radio"
        name="rating"
        value="Good"
        v-model="rating"
      /&amp;gt;
      Good&amp;lt;/label
    &amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data() {
    return {
      rating: "",
    };
  },
  computed: {
    activeColor() {
      return {
        bad: this.rating === "Bad",
        okay: this.rating === "Okay",
        good: this.rating === "Good",
      };
    },
  },
};
&amp;lt;/script&amp;gt;

&amp;lt;style&amp;gt;
.bad {
  color: red;
}

.okay {
  color: purple;
}

.good {
  color: green;
}
&amp;lt;/style&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
