Vue: How to change a value of state and use it in other page and change page structure at starting by it?

Viewed 207

In this project, we can login from login.vue by clicking login button and if it is success then we can see Lnb.vue in dashboard.vue

I thought if i code like this then pageSso will be 1 when I check the checkbox in login.vue in Lnb.vue then it will not show only "Account" menu. When I used console.log(pageSso) at mounted cycle it showed pageSso was 0. What would be the problem?

store/store.js

export const state = () => ({
    pageSso: 0,
})
export const getters = {
    pageSso: (state) => state.pageSso,
}

export const mutations = {
    setPageSso(state, data) {
        console.log('mutations setPageSso data', data)
        state.pageSso = data
    }
}

export const actions = {
    setPageSso({
        commit
    }, data) {
        console.log('actions setPageSso data', data)
        commit('setPageSso', data)
    },

}

pages/login.vue

<template>
<input
  class="checkbox_sso"
  type="checkbox"
  v-model="sso"
  true-value="1"
  false-value="0" >SSO checkbox
  <button class="point" @click="submit">login</button>
</template>
<script>
  export default {
    data() {
      return {
        sso: '',
      }
    },
    computed: {},
    methods: {
      submit() {
        this.$store.dispatch('store/setPageSso', this.sso)
        //this.$store.dispatch('store/login', data)
      },
</script>

pages/dashboard.vue

<template>
    <div class="base flex">
        <Lnb />
        <div class="main">
            <Gnb />
            <nuxt-child />
        </div>
    </div>
</template>
<script>
    import Lnb from '@/components/Lnb'
    import Gnb from '@/components/Gnb'
    export default {
        components: {
            Lnb,
            Gnb
        },
        mounted() {},
    }
</script>

components/Lnb.vue

<template>
  <ul>
    <li :class="{ active: navbarState == 7 ? true : false }">
      <a href="/dashboard/settings">
        <img src="../assets/images/ico_settings.svg" alt="icon" /> Settings
      </a>
    </li>
    <li v-show="pageSso != 1" :class="{ active: navbarState == 8 ? true : false }">
      <a href="/dashboard/user">
        <img src="../assets/images/ico_user.svg" alt="icon" />
        Account
      </a>
    </li>
  </ul>
</template>
<script>
  import {
    mapState
  } from 'vuex'
  export default {
    data() {
      return {}
    },
    computed: {
      ...mapState('store', {
        // navbarState: (state) => state.navbarState,

        pageSso: (state) => state.pageSso,
      }),
    },
    mounted() {
      console.log('pageSso ->', this.pageSso);
    },
    methods: {

    },
  }
</script>
1 Answers

Your console.log(pageSso) logs 0 because the mounted hook of Lnb.vue happens once, and it happens when the component is inserted into the DOM.

You insert Lnb into the DOM unconditionally in this line of dashboard.vue:

<Lnb />

and this is roughly when it's mounted hook is triggered.

Your pageSso seems to be changed only after you triggered the submit() method, which — I guess — happens way later, when you submit the login form.

Your Lnb.vue currently is always mounted. If you don't want to show it until pageSso is equal to 1, add a v-if on it in dashboard.vue like this:

<Lnb v-if="pageSso === 1" />

You currently don't have pageSso variable in dashboard.vue, you must take it from the store.

N.B.: Mind the difference between v-show and v-if directives: v-show only hides the component with display: none; while v-if actually removes or inserts the component from/to the DOM. With v-show, the component gets mounted even if you don't see it. With v-if, the component's mounted hook will fire each time the condition evaluates to true.

Related