0

Here's the template where my button and contactList1 reside:-

<template>
    <div class="chat-app">
    <button v-on:click="displayList1()">Contacts List 1</button>  //Button
        <Conversation :contact="selectedContact" :messages="messages" @new="saveNewMessage" v-bind:class="{conversation:conversation}" />
        <ContactsList :contacts="contacts" @selected="startConversationWith" v-bind:class="{contactsList1:contactsList1}"/>  //contactsList
       </div>
</template>

The object is default set to false

 data() {
            return {
                
                contactsList1: {
                    default: false,
                },
              
        },

Method:-

displayList1()
        {
            this.contactsList1 = false;

        },

Style:-

<style lang="scss" scoped>
.chat-app {
    display: flex;
}
.contactsList1 {
    background-color: black;
   
}
</style>

Even after the object being false the css is being applied, can anyone tell me what's wrong. I am just a beginner, Please help.

1 Answer 1

1

Your data function is returning the object contactsList1 and the full path to check the data type is this.contactsList1.default

You should also name your variables differently.

So here is a basic example on how to bind a Boolean datatype to your component class:

new Vue({
  el: "#app",
  data() {
    return {
      firstClass: {
        status: false
      }
    }
  },
  methods: {
    changeColour() {
      this.firstClass.status = true
    }
  }
  })
.styleFirstClass {
  background: red
}
.itemBox {
  padding:30px;
  margin:30px;
  border: 1px solid #444;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="changeColour()">Click to bind class</button>

  <div class="itemBox" :class="{styleFirstClass: firstClass.status}">
  This is some text
  </div>
  
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for such a well explained answer, It worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.