What is the reason Vue Router makes API calls from different routes when I am not currently within that route?

I am currently developing a Vue application where each route contains a table that pulls information from an API. The table is housed in a separate component and waits for an event from an eventBus to trigger a data refresh.

DataTable.vue

this.$bus.$on('reloadData', () => {
  this.getRecords()
})

Route A

<datatable></datatable>

Route B

<datatable></datatable>

Inner Component of Route B

 this.$bus.$emit('reloadData')

In my setup, the event is triggered from a modal component within Route B, but I have noticed that if I switch from Route A, C, or D to Route B, the event seems to be executed multiple times - once for each route visited before reaching Route B. This behavior gives the impression that components from other routes are active when they are not since I am only on Route B at the moment.

Answer №1

When you use <code>this.$bus.$on(...)
, a new event listener is created for each route visited. Since $bus is a global object, these listeners remain even when navigating away from the route. It is important to remove the old listener in the destroyed lifecycle hook of the component before navigation.

destroyed() {
    this.$bus.$off('reloadData');
}

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

Attempting to showcase extensive content based on the selection made in a drop-down menu

As a newcomer to anything beyond basic HTML, I am seeking guidance and assistance (preferably explained at a beginner level) for the following issue. If I have overlooked any crucial rules or concepts in my query, I apologize. I aim to have each selection ...

Display detailed images upon hovering

Top of the morning to everyone. I'm in search of a way to display higher resolution images when hovering over lower resolution ones. I want to create a rule or function that can handle this dynamically without manually adding hover effects and changi ...

Adjusting the content within a text area using an AngularJS service

I am currently editing text in a textarea within the admin view and I would like to display it through an angular service on the user view. However, I want the text to be displayed in multiple rows, maintaining the same format that I entered in the textare ...

What is the reason why createServer() is often not recognized as a function?

After installing express globally and npm on my express app, I am encountering issues with both intellisence and the app itself (I am using visual studio code on mac OS Yosemite). Below is a snippet of the code: /// <reference path="typings/node/node. ...

Performing live search with JQuery Ajax on two different types of fields - text input and dropdown select

Successfully implemented jQuery AJAX live search on an input field, returning desired results. Now aiming to create a compound search involving two fields. Current setup not yielding any results or errors: Form: <div class="form-group"> <div ...

Running zgrep using node.js

I'm looking to incorporate the zgrep command into my node.js app. Currently, I have successfully integrated the grep command as shown below: const grep = require('grep1'); grep(['greptext', './logs/file], function(err, stdou ...

Does anyone know of a way to integrate a calendar feature into a React application using a library

Greetings to everyone, I trust you are all enjoying a fantastic day. I am in search of an interactive calendar similar to this one for one of my applications https://i.sstatic.net/D3S3a.png Does anyone know of a React library that could assist me in crea ...

What is the method to store anchor text in a Session variable?

I'm currently working on a project where I've linked multiple pdf files in the master page. When clicking the anchor, the page redirects to the specified location and displays the pdf in an iframe. Now, I want the text within the anchor tag to be ...

Managing environment variables in Nuxt with Firebase: Best practices

I have been working on creating a firebase authentication app that is reusable and fully functional. However, despite exploring various online solutions, I have managed to develop a solution that works well. My concern now is about protecting sensitive dat ...

Link clicking event fails to trigger button clicking event on initial attempt in Internet Explorer. However, it works successfully on the second click

I'm feeling frustrated with a situation on my web page. I have a div that opens up in colorbox when clicked. Inside the div, there are links that should trigger an event in the code below. This event is meant to fill a hidden field and then click a se ...

Effective approach for managing a series of lengthy API requests

I am developing a user interface for uploading a list of users including their email and name into my database. After the upload process is complete, each user will also receive an email notification. The backend API responsible for this task is created u ...

Enhancing middleware chaining in Express

Below is the code for my Express configuration: var server = express() .use(express.cookieParser()) .use(express.session({secret: buffer.toString('hex')})) .use(express.bodyParser()) .use(express.static('./../')); serv ...

issue with angular directive not properly binding data

I am curious about the following code: HTML: <div class="overflow-hidden ag-center" world-data info="target"></div> js: .directive('worldData', ['$interval', function($interval) { return { scope: { ...

Spinning a basic square two times

I am somewhat familiar with Blender 3D, but I have very little experience with 3D programming and I am currently trying to work with three.js. My query is this: I have a basic cube that I rotated around the y-axis; now I need to rotate it again around the ...

Error Alert: Invalid Hook Call detected while using the useSelector hook within a Functional Component

I am encountering an error Error: Invalid hook call. Hooks can only be called inside of the body of a function component. The versions of React and the renderer (such as React DOM) might not match There could be violations of the Rules of Hooks Multiple ...

Creating IPv6 Mask from IPv6 Prefix Using Javascript: A Step-by-Step Guide

Write a JavaScript/TypeScript function that can convert IPv6 prefixes (ranging from 0 to 128) into the corresponding mask format (using the ffff:ffff style). Here are some examples: 33 => 'ffff:ffff:8000:0000:0000:0000:0000:0000' 128 => ...

What is the best way to work with JObject when the property names have small variations?

After extracting data from my MVC Form in the View using JavaScript, I have a JObject obtained from JSON.NET that looks like this: var jOBject = {"schedule.ID" : 1, "schedule.Name" : "NameSchedule"} My goal is to convert this JObject into a Schedule obj ...

Is it possible to utilize the Vue DEVTOLLS plugin to modify the vuex state in a live production environment?

Incorporating a login feature into a vue.js platform has led me to save the logged in user's status in userIsAuthenticated. This has made me wonder: Is it safe to utilize the Vue DEVTOLLS plugin to modify the vuex state in a live production applicatio ...

JavaScript Array Problem

Could you please review the code below and help me understand why I am encountering issues when trying to run the program? $(document).ready(function() { var comp = new Array("AAPL", "MSFT", "XRTX&"); var t = setInterval(function(){ ...

A step-by-step guide on how to use the Google Closure Compiler: a

Is there anyone who could assist me in adding a snippet to the basic process of Google Closure Compiler? I have been trying unsuccessfully to do this via JavaScript code. I am using an example snippet from the official npm page, but when I run it, someth ...