简体   繁体   中英

Why component doesn't render when I change the route?

I have a template, and there is a component:

        <super-visor
            v-if="!top10ModeActivated"
            v-for="cart in getCarts(index + 1)"
            :cart="cart"
            :key="cart.position"
        ></super-visor>

As you can see, this component renders when top10ModeActivated is only false ;

   computed: {
        top10ModeActivated() {
            return this.$store.state.moduleSearch.top10ModeActivated;
        },
   }

I put debugger to top10ModeActivated and it works only when component renders only for the first time. So I see my component, only when I refresh the page but not when I change the route.

Can anybody help me and describe me how I can fix this? Because I'm new to VueJS.

Use methods because computed properties are cached . See below:

   methods: {
        top10ModeActivated() {
            return this.$store.state.moduleSearch.top10ModeActivated;
        },
   }

and

        <super-visor
            v-if="!top10ModeActivated()"
            v-for="cart in getCarts(index + 1)"
            :cart="cart"
            :key="cart.position"
        ></super-visor>

Accessing store state directly from computed property doesn't make it reactive. Use getters instead. Create a Vuex getter which would return top10ModeActivated and then call that getter in a computed property to have it reactive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM