Steps to trigger a mutation in the store every time the route is modified

Is it possible to trigger a mutation in the store every time the route changes, without relying on a Vuex plugin to detect route/ mutations?

This scenario involves using vuex, nuxt, vue-router, and vue-router-sync.

Clarification: I am attempting to commit the mutation to a different part of the store, not just the route segment.

There are many inquiries about the opposite situation, but this question pertains to a unique scenario, so please consider this before marking it as a duplicate ;).

Answer №1

To trigger a mutation, you can set up an afterEach listener on the router and invoke the mutation from there:

router.afterEach((to, from) => {
  store.commit('UPDATE_DATA', ...)
})

If you are working with Nuxt, you can try the following approach:

// plugins/trigger.js

export default ({ app: { router, store } }) => {
  router.afterEach((to, next) => {
    store.commit('UPDATE_DATA', ...)
  })
}
// nuxt.config.js

plugins: [
  '~/plugins/trigger'
]

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

Tips for Establishing Communication Between Two Dynamic Canvas Elements

How do I establish communication between two animated canvas elements? I have created two HTML5 canvas animations using Adobe Animate CC. Both of these animations are placed on a single HTML page. I am able to call functions from within the animations and ...

Utilize a map image as a texture on a plane within a personalized shader in THREE.js

I'm currently facing an issue where I need to load two images as textures, blend between them in the fragment shader, and apply the resulting color to a plane. However, I am struggling to even display a single texture properly. My process for creatin ...

JavaScript inquiry

How do you typically utilize JavaScript in your projects? I often use it for form validation, creating dropdown menus, and more. Do you have any unique ways of using JavaScript? Personally, I find jQuery to be a superior tool in certain situations... ...

What is the best way to retrieve the latest prop from a parent component in Vue.js after invoking a method?

Within my code, there is an event emitter that triggers an API call in the parent component: Parent component <ProductChild :productId="productId" @update="getProduct()" :product="product" ...

JavaScript Tutorial: Storing HTML Text in the HTML5 Local Storage

I am looking to save the text that is appended using html() every time I click on a button. The goal is to store this text in HTML 5 local storage. $('#add_new_criteria').on('click',function(){ $('#cyc_append').html(&apo ...

Loading HTML content in a WPF WebBrowser without encountering security messages

Currently, I am developing a WPF application in which I create the content of an HTML file as a string (including some JavaScript functions for calculations). After generating the string, I save it as an HTML file on my local disk and then reload it using ...

What is the best way to execute a script on the homepage just one time?

I need some assistance with a script that should only run when a visitor arrives at my site for the first time. I want all content to be hidden initially and then revealed in a visually appealing way when the visitor clicks a button. However, I do not want ...

Step-by-step guide for setting up CodeMirror

I'm feeling a bit lost with what to do next: <link rel="stylesheet" href="lib/codemirror.css"> <script src="lib/codemirror.js"></script> <script> var editor = CodeMirror.fromTextArea(myTextarea, { mode: "text/html" }); ...

Avoiding duplication of prints in EJS template files

In my EJS code, I have created a loop to fetch the total amount of items from the database. Here is my current code: <h2>Summary</h2> <% if(typeof items.cart!=="undefined"){ var amount = 0; %> <% i ...

Automatically append version number to requests to avoid browser caching with Gulp

When deploying my project, I utilize gulp to build the source files directly on the server. In order to avoid caching issues, a common practice is to add a unique number to the request URL as explained in this article: Preventing browser caching on web app ...

Filling the void through webpack2 and the powerful webpack.LoaderOptionsPlugin

I'm new to using webpack. It seems like the vue-loader hasn't been updated to work with webpack 2. I tried using the example helper (LoaderOptionsPlugin), but I'm having trouble getting it to function properly. Here's the error message ...

Eliminate any unnecessary tags located before the text

I am facing a challenge with the following code snippet. The Variable contains a string that includes HTML tags such as <img>, <a>, or <br>. My goal is to eliminate the <img> tag, <a> tag, or <br> tag if they appear befo ...

Transferring NodeJS information to JavaScript

I am currently working on a web application where the client receives data from a server. The challenge I'm facing is how to pass this data from NodeJS to a Javascript file that is included in an HTML document. Unfortunately, I have not been successfu ...

Exploring the Wonders of React Memo

I recently started delving into the world of React. One interesting observation I've made is that when interacting with componentized buttons, clicking on one button triggers a re-render of all button components, as well as the parent component. impo ...

A method for utilizing wildcards in conjunction with the .load function

Currently, I am utilizing jquery, jquery mobile, js, html, and css within Phonegap to develop my android/ios application. However, my understanding of js & jquery is somewhat limited. In my index.html file, I use .load to load other html files into s ...

How can we develop a strategy to select products based on specific features while keeping costs minimized?

I've got a collection of products with varying costs and features. Each product offers a unique set of features. I'm in need of an algorithm that, when given the specific features I desire, can recommend the most cost-effective combination of pr ...

Avoiding redundant code in React Components: Best practices to keep your code DRY

I am currently utilizing React with Material UI v1.0 to implement a list, but I want to avoid code repetition. Here is the existing code: import List from 'material-ui/List'; import DashboardIcon from 'material-ui-icons/Dashboard'; ...

Postman successfully validates the API, however, it experiences issues when run in Mocha JS

When testing my API with Postman, everything works fine. However, when I try to test the same API with Mocha JS using the same data, I am encountering errors such as "500 internal server error" and "400 bad request". I have double-checked that I am passin ...

How can you enhance functionality in Polymer without the need to create a custom element?

This is the code snippet from my index.html file in Polymer 1.1: <template is="dom-bind" id="app"> <paper-drawer-panel force-narrow id="the-drawer"> <paper-header-panel drawer> <paper-toolbar></paper-too ...

Tips for bringing a particular tab into focus when a page initially loads

My webpage features custom links (tabs) that display content when clicked. By default, the first tab is selected and its content is shown, while the rest are set to display:none. Clicking on any other link will assign the 'selected' class to it, ...