Vue Watch not Triggering on Page Refresh/Reload The Next CEO of Stack OverflowAccessing Vuex state when defining Vue-Router routesVue.js - Making helper functions globally available to single-file componentsUsing vue-router in Vuex storeVue.js w vuex : mocked action not executedv-if not triggered on computed propertyVuex this.$store is undefined in child componentGet store value in non-child component in Vue and VuexJest : How to mock vue-router used in vuexvue-router can't access my componentUsing VueX $store to Vue Router

If the heap is initialized for security, then why is the stack uninitialized?

Why is there a PLL in CPU?

Increase performance creating Mandelbrot set in python

How should I support this large drywall patch?

Science fiction (dystopian) short story set after WWIII

Why do remote companies require working in the US?

Is it safe to use c_str() on a temporary string?

What does "Its cash flow is deeply negative" mean?

How do I construct this japanese bowl?

How to Reset Passwords on Multiple Websites Easily?

Why didn't Khan get resurrected in the Genesis Explosion?

Why does standard notation not preserve intervals (visually)

Can the Reverse Gravity spell affect the Meteor Swarm spell?

How do scammers retract money, while you can’t?

Is a stroke of luck acceptable after a series of unfavorable events?

Inappropriate reference requests from Journal reviewers

Can a single photon have an energy density?

How to make a software documentation "officially" citable?

Why does C# sound extremely flat when saxophone is tuned to G?

What do "high sea" and "carry" mean in this sentence?

What is the purpose of the Evocation wizard's Potent Cantrip feature?

Can I equip Skullclamp on a creature I am sacrificing?

Fastest way to shutdown Ubuntu Mate 18.10

Why did we only see the N-1 starfighters in one film?



Vue Watch not Triggering on Page Refresh/Reload



The Next CEO of Stack OverflowAccessing Vuex state when defining Vue-Router routesVue.js - Making helper functions globally available to single-file componentsUsing vue-router in Vuex storeVue.js w vuex : mocked action not executedv-if not triggered on computed propertyVuex this.$store is undefined in child componentGet store value in non-child component in Vue and VuexJest : How to mock vue-router used in vuexvue-router can't access my componentUsing VueX $store to Vue Router










0















I have a component which is hid based on the route which is active, it kicks off a function which is stored using vuex store.



It works as intended, the sidenav is hidden on login, logout, and register.



However, I noticed when I am on an authenticated page such as admin panel, or dashboard, etc, the component displays correctly, but when/if someone reloads the webpage, the component disappears, only to be displayed when clicking a link to another page.



App.Vue



<template>
<div id="app">
<navbar />
<sidenav v-show="sidenav_toggle" />
<div class="row router-container">
<div class="col router-row">

<router-view/>
</div>
</div>
</div>
</template>
<script>
import Vue from 'vue'
import Vuex from 'vuex'
import router from '@/router'

import axios from 'axios'
import AxiosStorage from 'axios-storage'

let sessionCache = AxiosStorage.getCache('localStorage');



import materializecss from '../static/css/main.css'
import materializejs from '../static/materialize-css/dist/js/materialize.js'

import navbar from '@/components/navbar'
import sidenav from '@/components/sidenav'

Vue.use(Vuex)

const state =
sidenav:
show: false



const mutations =
show_sidenav(state)
state.sidenav.show = true
,
hide_sidenav(state)
state.sidenav.show = false



const store = new Vuex.Store(
state,
mutations
)



export default
router,
name: 'App',
watch:
$route: function()
if(this.$route.path === '/login' ,
deep: true,
immediate: true
,
computed:
sidenav_toggle()
return store.state.sidenav.show

,

data: function()
return

,
components:
navbar,
sidenav
,
methods:


,
created: function()



</script>









share|improve this question


























    0















    I have a component which is hid based on the route which is active, it kicks off a function which is stored using vuex store.



    It works as intended, the sidenav is hidden on login, logout, and register.



    However, I noticed when I am on an authenticated page such as admin panel, or dashboard, etc, the component displays correctly, but when/if someone reloads the webpage, the component disappears, only to be displayed when clicking a link to another page.



    App.Vue



    <template>
    <div id="app">
    <navbar />
    <sidenav v-show="sidenav_toggle" />
    <div class="row router-container">
    <div class="col router-row">

    <router-view/>
    </div>
    </div>
    </div>
    </template>
    <script>
    import Vue from 'vue'
    import Vuex from 'vuex'
    import router from '@/router'

    import axios from 'axios'
    import AxiosStorage from 'axios-storage'

    let sessionCache = AxiosStorage.getCache('localStorage');



    import materializecss from '../static/css/main.css'
    import materializejs from '../static/materialize-css/dist/js/materialize.js'

    import navbar from '@/components/navbar'
    import sidenav from '@/components/sidenav'

    Vue.use(Vuex)

    const state =
    sidenav:
    show: false



    const mutations =
    show_sidenav(state)
    state.sidenav.show = true
    ,
    hide_sidenav(state)
    state.sidenav.show = false



    const store = new Vuex.Store(
    state,
    mutations
    )



    export default
    router,
    name: 'App',
    watch:
    $route: function()
    if(this.$route.path === '/login' ,
    deep: true,
    immediate: true
    ,
    computed:
    sidenav_toggle()
    return store.state.sidenav.show

    ,

    data: function()
    return

    ,
    components:
    navbar,
    sidenav
    ,
    methods:


    ,
    created: function()



    </script>









    share|improve this question
























      0












      0








      0








      I have a component which is hid based on the route which is active, it kicks off a function which is stored using vuex store.



      It works as intended, the sidenav is hidden on login, logout, and register.



      However, I noticed when I am on an authenticated page such as admin panel, or dashboard, etc, the component displays correctly, but when/if someone reloads the webpage, the component disappears, only to be displayed when clicking a link to another page.



      App.Vue



      <template>
      <div id="app">
      <navbar />
      <sidenav v-show="sidenav_toggle" />
      <div class="row router-container">
      <div class="col router-row">

      <router-view/>
      </div>
      </div>
      </div>
      </template>
      <script>
      import Vue from 'vue'
      import Vuex from 'vuex'
      import router from '@/router'

      import axios from 'axios'
      import AxiosStorage from 'axios-storage'

      let sessionCache = AxiosStorage.getCache('localStorage');



      import materializecss from '../static/css/main.css'
      import materializejs from '../static/materialize-css/dist/js/materialize.js'

      import navbar from '@/components/navbar'
      import sidenav from '@/components/sidenav'

      Vue.use(Vuex)

      const state =
      sidenav:
      show: false



      const mutations =
      show_sidenav(state)
      state.sidenav.show = true
      ,
      hide_sidenav(state)
      state.sidenav.show = false



      const store = new Vuex.Store(
      state,
      mutations
      )



      export default
      router,
      name: 'App',
      watch:
      $route: function()
      if(this.$route.path === '/login' ,
      deep: true,
      immediate: true
      ,
      computed:
      sidenav_toggle()
      return store.state.sidenav.show

      ,

      data: function()
      return

      ,
      components:
      navbar,
      sidenav
      ,
      methods:


      ,
      created: function()



      </script>









      share|improve this question














      I have a component which is hid based on the route which is active, it kicks off a function which is stored using vuex store.



      It works as intended, the sidenav is hidden on login, logout, and register.



      However, I noticed when I am on an authenticated page such as admin panel, or dashboard, etc, the component displays correctly, but when/if someone reloads the webpage, the component disappears, only to be displayed when clicking a link to another page.



      App.Vue



      <template>
      <div id="app">
      <navbar />
      <sidenav v-show="sidenav_toggle" />
      <div class="row router-container">
      <div class="col router-row">

      <router-view/>
      </div>
      </div>
      </div>
      </template>
      <script>
      import Vue from 'vue'
      import Vuex from 'vuex'
      import router from '@/router'

      import axios from 'axios'
      import AxiosStorage from 'axios-storage'

      let sessionCache = AxiosStorage.getCache('localStorage');



      import materializecss from '../static/css/main.css'
      import materializejs from '../static/materialize-css/dist/js/materialize.js'

      import navbar from '@/components/navbar'
      import sidenav from '@/components/sidenav'

      Vue.use(Vuex)

      const state =
      sidenav:
      show: false



      const mutations =
      show_sidenav(state)
      state.sidenav.show = true
      ,
      hide_sidenav(state)
      state.sidenav.show = false



      const store = new Vuex.Store(
      state,
      mutations
      )



      export default
      router,
      name: 'App',
      watch:
      $route: function()
      if(this.$route.path === '/login' ,
      deep: true,
      immediate: true
      ,
      computed:
      sidenav_toggle()
      return store.state.sidenav.show

      ,

      data: function()
      return

      ,
      components:
      navbar,
      sidenav
      ,
      methods:


      ,
      created: function()



      </script>






      vue.js vuex vue-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 14:07









      Andrew BakerAndrew Baker

      83




      83






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Your watcher is not called if you land directly on the admin page because the $route property never changes (and watchers only watch for changes).



          What you could do is move your watcher function in a method, and call this method in the created hook and in your watcher.



          An even better way to do this would be to use vue-router navigation-guards



          Example:



          export default 
          // ...
          methods:
          adaptSidebar(path)
          if (['/login', '/logout', '/register'].includes(path))
          store.commit('hide_sidenav')
          else
          store.commit('show_sidenav')

          ,
          ,
          beforeRouterEnter(from, to, next)
          // As stated in the doc, we do not have access to this from here
          next(vm =>
          vm.adaptSidebar(to.path)
          )
          ,
          beforeRouteChange(from, to, next)
          this.adaptSidebar(to.path)
          ,






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55045738%2fvue-watch-not-triggering-on-page-refresh-reload%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Your watcher is not called if you land directly on the admin page because the $route property never changes (and watchers only watch for changes).



            What you could do is move your watcher function in a method, and call this method in the created hook and in your watcher.



            An even better way to do this would be to use vue-router navigation-guards



            Example:



            export default 
            // ...
            methods:
            adaptSidebar(path)
            if (['/login', '/logout', '/register'].includes(path))
            store.commit('hide_sidenav')
            else
            store.commit('show_sidenav')

            ,
            ,
            beforeRouterEnter(from, to, next)
            // As stated in the doc, we do not have access to this from here
            next(vm =>
            vm.adaptSidebar(to.path)
            )
            ,
            beforeRouteChange(from, to, next)
            this.adaptSidebar(to.path)
            ,






            share|improve this answer



























              0














              Your watcher is not called if you land directly on the admin page because the $route property never changes (and watchers only watch for changes).



              What you could do is move your watcher function in a method, and call this method in the created hook and in your watcher.



              An even better way to do this would be to use vue-router navigation-guards



              Example:



              export default 
              // ...
              methods:
              adaptSidebar(path)
              if (['/login', '/logout', '/register'].includes(path))
              store.commit('hide_sidenav')
              else
              store.commit('show_sidenav')

              ,
              ,
              beforeRouterEnter(from, to, next)
              // As stated in the doc, we do not have access to this from here
              next(vm =>
              vm.adaptSidebar(to.path)
              )
              ,
              beforeRouteChange(from, to, next)
              this.adaptSidebar(to.path)
              ,






              share|improve this answer

























                0












                0








                0







                Your watcher is not called if you land directly on the admin page because the $route property never changes (and watchers only watch for changes).



                What you could do is move your watcher function in a method, and call this method in the created hook and in your watcher.



                An even better way to do this would be to use vue-router navigation-guards



                Example:



                export default 
                // ...
                methods:
                adaptSidebar(path)
                if (['/login', '/logout', '/register'].includes(path))
                store.commit('hide_sidenav')
                else
                store.commit('show_sidenav')

                ,
                ,
                beforeRouterEnter(from, to, next)
                // As stated in the doc, we do not have access to this from here
                next(vm =>
                vm.adaptSidebar(to.path)
                )
                ,
                beforeRouteChange(from, to, next)
                this.adaptSidebar(to.path)
                ,






                share|improve this answer













                Your watcher is not called if you land directly on the admin page because the $route property never changes (and watchers only watch for changes).



                What you could do is move your watcher function in a method, and call this method in the created hook and in your watcher.



                An even better way to do this would be to use vue-router navigation-guards



                Example:



                export default 
                // ...
                methods:
                adaptSidebar(path)
                if (['/login', '/logout', '/register'].includes(path))
                store.commit('hide_sidenav')
                else
                store.commit('show_sidenav')

                ,
                ,
                beforeRouterEnter(from, to, next)
                // As stated in the doc, we do not have access to this from here
                next(vm =>
                vm.adaptSidebar(to.path)
                )
                ,
                beforeRouteChange(from, to, next)
                this.adaptSidebar(to.path)
                ,







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 10 at 20:05









                PhilPhil

                1114




                1114





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55045738%2fvue-watch-not-triggering-on-page-refresh-reload%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко