The vuex store does not activate in routed components

After setting up my vuex store and ensuring that everything is functioning properly, I encountered an issue where I can only commit mutations in components that are directly imported into src App.vue.

For instance, the resetState function in Header.vue successfully triggers when I click the button because it is imported directly in src/App.vue

<template>
      <v-btn
        icon
        to="/"
        id="no-background-hover"
        @click="resetState"
      >
          ///
      </v-btn>
</template>

<script lang="ts">
import Vue from 'vue'
import { MutationTypes } from '@/store/mutation-types'

export default Vue.extend({
  name: 'Header',

  methods: {
    resetState () {
      this.$store.commit(MutationTypes.RESET_STATE, 0)
    }
  },

})
</script>

However, when the same component is present in any of my routed views/BlahBlahBlah.vue, nothing happens upon clicking the button

Below is the content of main.ts

import Vue from 'vue'
import VueTypedJs from 'vue-typed-js'

import App from './App.vue'
import router from './router'
import './assets/sass/index.sass'
import store from './store'
import vuetify from './plugins/vuetify'

Vue.config.productionTip = false
Vue.use(VueTypedJs)
new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount('#app')

Answer №1

The issue you are facing stems from the fact that the "to="/" is executed before accessing the store, and due to the page refresh, the command is not being sent.

Here's a proper way to handle it:

<template>
      <v-btn
        icon
        id="no-background-hover"
        @click="resetState"
      >
          ///
      </v-btn>
</template>

<script lang="ts">
import Vue from 'vue'
import { MutationTypes } from '@/store/mutation-types'

export default Vue.extend({
  name: 'Header',

  methods: {
    resetState () {
      this.$store.commit(MutationTypes.RESET_STATE, 0)
      window.location.href = '/'
    }
  },

})
</script>

Answer №2

After some troubleshooting, I discovered the solution. It turns out that enclosing router-view within a keep-alive tag did the trick.

<keep-alive>
        <router-view/>
</keep-alive>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamically divide canvas screens based on fabricjs dropdown selection

I am attempting to implement split screens in fabric js, such as 1, 2, 4, 8, and 16. The screen should split based on the selection from the dropdown menu. Check out my current code where I have successfully uploaded images. If I click on the images, th ...

Create an interactive mechanism where one scrollbar dynamically adjusts another scrollbar and vice versa

Using the provided js-bin, how can I synchronize scrolling between the "left" div and the "right" div when using the mouse wheel? Currently, if you uncomment the code block, the scrolling behavior changes from scrolling by 100px per scroll to scrolling pi ...

What sets apart object destructuring from destructuring assignment?

Consider this scenario where we have an object: let obj = { a: 1, b: 2 } let { a, b } = obj; console.log(a, b); // output 1, 2 Now, let's examine a different case where 'a' and 'b' are already initialized: let obj = { a: 1, b: 2 ...

Tips for effectively transmitting data while utilizing a declarative/reactive data access method with RxJS in Angular?

Angular typically follows a classic pattern for data access that looks like this: Traditional Data Access Pattern getProducts(): Observable<Product[]> { return this.http.get<Product[]>(this.productsUrl) .pipe( tap(data => consol ...

Using JavaScript to sort through an array of nested objects

I'm working on extracting data from a deeply nested array. Here's the initial setup: layout: { parent: [ { type: "tabset", id: "tab1", children: [ { type: "tab", id: "id1& ...

The dual-file upload feature of the jQuery ajaxForm plugin

After extensive searching online, I have yet to find an answer to my problem. The issue at hand is that when using the ajaxForm malsup plugin, it submits my files twice. HTML: <form id="gallery" enctype="multipart/form-data" action="/upload-gallery.p ...

Utilizing a combination of jQuery and JavaScript, construct an innovative image slider to meet

I'm working on creating a slider that allows users to navigate through reviews by clicking arrows, moving from review1 to review2 and so on. I've set up my HTML with the reviews and my CSS with hidden values for now. Any assistance would be great ...

Create a standalone 404 page using React Router that is completely isolated from any other components

Currently, I am collaborating with <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfadbabebcabbaadf2adb0aaabbaadf2bbb0b29fe9f1ebf1ed">[email protected]</a> and the code I am working on looks like this: index.tsx ...

Error: Unable to locate module '@/components/Header' in Next.js project

I'm currently facing an issue while working on my Next.js project. The problem arises when I attempt to import a component into my 'page.js' file. In the 'src' folder, I have a subdirectory named 'components' which contai ...

Is it possible to reveal a concealed element or modify the functionality of a hyperlink?

I have a link in the navigation that includes an animated icon of a + turning into an x. When the icon is in the x state, I want users to be able to close it by clicking on the icon or text. However, I am unsure of the best way to approach this. One op ...

How should we structure our JavaScript code: MVC or self-rendering components?

I'm in the process of developing a highly JS-centric web application. The bulk of the work is being carried out on the client side, with occasional syncing to the server using AJAX and XMPP. This is my first venture into creating something of this ma ...

Should CSS variables be defined within the render method of a React component?

Yesterday, I mistakenly created and deleted a post. Now, I am facing an issue with creating a component that requires specific styling. The problem arises when the componentDidMount() method causes flickering during the rendering process, as it takes some ...

The Measure-x Annotation toolbar in High stocks is conspicuously absent from the graph, instead making an appearance in the navigator section below

Recently, I encountered a strange issue that has left me puzzled. When I trigger the measure-x event on my graph, the annotation and its toolbar are not displaying on the graph as expected. Instead, they appear on the navigator below. The annotation should ...

Adjusting column widths in Material-Table: A guide to resizing columns

I am currently using the Material Table library, recommended by Google Material UI as a top data table library. I am facing some issues related to configuring the width of columns in this library. The column's `width` property seems to be functioning ...

What are some methods for enabling communication between two sibling components without altering the parent's state?

Recently diving into ReactJS, I encountered an issue that's been puzzling me. I have a Parent component with two child components - List and Map, each containing their own child items. What I'm trying to achieve is to change the style of the corr ...

How can you retrieve the property value from an object stored in a Set?

Consider this scenario: SomeItem represents the model for an object (which could be modeled as an interface in Typescript or as an imaginary item with the form of SomeItem in untyped land). Let's say we have a Set: mySet = new Set([{item: SomeItem, s ...

`How to utilize the spread operator in Angular 4 to push an object to a specific length`

One issue I'm facing is trying to push an object onto a specific index position in an array, but it's getting pushed to the end of the array instead. this.tradingPartner = new TradingPartnerModel(); this.tradingPartners = [...this.tradingPartner ...

Preserve all the impact of a website indefinitely

Imagine a scenario where a webpage contains a button that, when pressed, creates another button below it. How can you save this effect so that when you reopen the webpage, the new button is still displayed below the original one? Is there a way to store ...

Automatically updating the date value in one matDatePicker field will impact the date in another matDatePicker field within a Reactive Form

I am struggling with dynamically updating the start date value in my reactive form, which requires setting the value of the start date field based on the statement date field value and a selected number of months. <div [formGroup]="parameters" ...

Searching through a JSON object for nested objects within objects

Currently, I have some data structured as follows: var items = [ { "id" : 1, "title" : "this", "groups" : [ {"id" : 1, "name" : "groupA"}, {"id" : 2, "name" : "groupB"} ] }, { "id" : 2, "title" : "that", ...