search

Home  >  Q&A  >  body text

How to use two conditional statements in VueJS

I'm creating a sidebar with a lot of functionality so I need to add two conditions to my :class but I don't know how to do it. This is what I did:

:class=" showSidebar ? 'open' : 'closed', showSidebarMini ? 'openmini' : 'closedmini'"

P粉877719694P粉877719694286 days ago370

reply all(2)I'll reply

  • P粉441076405

    P粉4410764052024-02-18 11:15:08

    You can use object styles to do the following:

    Example:

    {
      'open': showSidebar,
      'closed': !showSidebar,
      'openmini': showSidebarMini,
      'closedmini': !showSidebarMini
    }
    

    In your case:

    :class="{ 'open': showSidebar, 'closed': !showSidebar, 'openmini': showSidebarMini, 'closedmini': !showSidebarMini }"
    

    reply
    0
  • P粉333395496

    P粉3333954962024-02-18 09:41:09

    You can use class array:

    :class="[(showSidebar ? 'open' : 'closed'), (showSidebarMini ? 'openmini' : 'closedmini')]"

    reply
    0
  • Cancelreply