2

Reading the documentation I don't understand how I can change a class.

My component data looks like this:

props: [
  'm_c_id',
  'recipient_id',
  'sender_id',
  'userId'
],

Now if sender_id == userId I want to show this in my template:

<div class="msg-from-me">
   <p>Hello</p>
</div>

or if recipient_id == userId I want to show this:

<div class="msg-from-them">
   <p>Hello</p>
</div>
4
  • 1
    This is covered well in the documentation. Commented Jul 3, 2017 at 19:25
  • @BertEvans is it like this? v-bind:class="{'msg-from-me': userId==m_sender_id, 'msg-from-them': userId==m_recipient_id}" Commented Jul 3, 2017 at 19:26
  • 2
    Possible duplicate of Vue.js: Conditional class styling Commented Jul 3, 2017 at 19:27
  • Yes, like that. Commented Jul 3, 2017 at 19:27

1 Answer 1

3

You can also use the ternary operator to toggle classes:

<div v-bind:class="[sender_id === userId ? msg-from-me : msg-from-them]">

This way, if the only two options are sender_id or recipient_id, the proper class will toggle.

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

Comments

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.