Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue change width and content

I am using vuejs and depending on whether the user is logged in, I need to adjust the size and content of two divs inside my topbar. So if they aren't logged in it should be like this:

<div id='search' width="400px"></div><div id="login" width="200px"><img></div>

And when they're logged in it should be like this:

<div id='search' width="200px"></div><div id="login" width="400px"><div id='somecontent></div><div id='morecontent'></div></div>

I know i can achieve this by hardcoding both of them entirely and then using v-if statements but I was wondering if there was a better way.

like image 257
JFugger_jr Avatar asked Aug 17 '17 08:08

JFugger_jr


2 Answers

<div id='search' :style="{width: loggedIn ? '200px' : '400px'}"></div>
<div id="login" :style="{width: loggedIn ? '400px' : '200px'}">
  <div id='somecontent' v-if="loggedIn"></div>
  <div id='morecontent' v-if="loggedIn"></div>
  <img v-if="!loggedIn">
</div>

You can bind style in vuejs by using v-bind

new Vue({
  ...
  data: {
    loggedIn: false
  }
  ...
})

fiddle

like image 85
choasia Avatar answered Nov 12 '22 06:11

choasia


create a default width inside your data with the default value, like:

data() {
        return {
            myWidth: '200'
        }
    },

everytime you login you should change the width value, and then you can do something like this:

<div :style="{ width: myWidth + 'px' }" id='search' width="400px"></div>

hope it helps!

like image 30
Filipe Costa Avatar answered Nov 12 '22 06:11

Filipe Costa