PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 16, 2022

[FIXED] How can I add conditional style in Vue.js?

 October 16, 2022     css, vue.js     No comments   

Issue

I want to add a conditional style in my Component. My component is below:

    <site-pricing
        color="primary"
        currency="$"
        price="25"
        to="/purchase"
>
        <template v-slot:title>Complete</template>
        <template v-slot:desc>{{ $t('plans.completeDesc' )}}</template>
        <template v-slot:planSuffix>/ {{ $t('plans.oneTime' )}}</template>
        <template v-slot:features>
          <ul>
            <li>{{ $t('plans.xchats', [ 200 ] )}}</li>
            <li>{{ $t('plans.sDomain' )}}</li>
          </ul>
        </template>
        <template v-slot:footer>{{ $t('plans.oneTime' )}}</template>
</site-pricing>

I want to add a special style just like, if 'color = primary', then add a border-top: 5px red...


Solution

You should use the conditional style binding for vue. I'll show an example:

new Vue({
  el: '#app',
  data: {
    color: "secondary"
  },
  methods: {
    toggleColor(val) {
      this.color = val
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div :style="[color==='primary' ? {color: 'red'} : {color: 'blue'}]">Primary</div>
<div :style="[color==='secondary' ? {color: 'red'} : {color: 'blue'}]">Secondary</div>
<button @click="(e) => toggleColor('primary')">Switch to pirmary</button>
<button @click="(e) => toggleColor('secondary')">Switch to secondary</button>
</div>



Answered By - Daniyal Lukmanov
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing