Vue keeps reloading the page twice

I've been facing challenges with writing Vue code. Currently, I am utilizing both Vue and Vuetify frameworks.

There are A and B pages in the application. It works fine when navigating to either the A or B page individually.

However, an issue arises when the sequence of navigation is as follows: A -> B -> A.

menuselector.vue

<template>
   <v-list>
      <template v-for='(eachmenu) in menu'>
         <v-list-item
            :to='eachmenu.path'
         >
            <v-list-item-title>
              {{eachmenu.title}}
            </v-list-item-title>
         </v-list-item>
      </template>
   </v-list>
</template>

<script>
export default {
   name: 'selector',
   data() {
      return {
         menu: [
            {
               title: 'A',
               path: '/A',
            },
            {
               title: 'B',
               path: '/B',
            }
         ]
      }
   }
}
</script>

router.vue

export default new Router({
   mode: 'history',
   base: process.env.BASE_URL,
   routes: [
      {
         path: '/',
         redirect: '/A',
         component: TestComponent,
         children: [
            {
               path: 'A',
               component: () => import('@/component/A.vue'),
               name: 'Acomponent',
            },
            {
               path: 'B',
               component: () => import('@/component/B.vue'),
               name: 'Bcomponent',
            }
         ]
      }
   ]
})

A.vue&B.vue

<template>
   test
</template>

<script>
export default {
   beforeCreate() {
      console.log('beforeCreate');
   },
   created() {
      console.log('created');
   }
}
</script>

The console output displays the following:

https://i.sstatic.net/KB7QU.png

What could be causing this issue?

Answer №1

  1. The term "loading" may not be suitable in this context. The required code for the component is fetched from the server only once, as evident from monitoring the Dev Tools Network tab.
  2. During route transitions, the component for the previous route is destroyed and a new component for the new route is created. This behavior is inherent to Vue's dynamic component handling (utilized by <router-view>). If you wish to maintain component state, consider using <keep-alive>. It's crucial to review the documentation and understand the potential increase in memory usage
  <keep-alive>
    <router-view :key="$route.fullPath"></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

Ensure that the CSS fade-in effect occurs every time the element is clicked, not just the first time

Is it possible to make text fade in on every click instead of just the first time by using CSS transitions and JavaScript? My current progress: <style> #data { transition: .7s; } </style> <h1 id="data"></h1> <a hr ...

Is there a way to display the "back button" on a subview?

As a newcomer to Ionic, I am finding navigation to be the most challenging aspect. My app has two tabs - "Dashboard" and "Friends". When I click on the Dashboard tab, I want it to navigate to a subview called "subview_dash", without displaying the tabs in ...

Displaying only one piece of information instead of multiple pieces of information from a row in Javascript

Showing the CSV data that was retrieved, with the desired output as follows: Price: 955.99 EPS: 29.59 Date : 7/14/2017 Unfortunately, the actual output appears like this: https://i.sstatic.net/zMVCo.png The data is displayed in a row, separated by col0 ...

Angular 2: Applying class to td element when clicked

I am working with a table structured like this <table> <tbody> <tr *ngFor="let row of createRange(seats.theatreDimension.rowNum)"> <td [ngClass]="{'reserved': isReserved(row, seat)}" id={{row}}_{{sea ...

Show a single jQuery Validation error

I am struggling with displaying an error message at the top if validation fails, without using the errorPlacement option. Instead, I want to showcase the message in a Bootstrap alert box. Below is the HTML code snippet: <form method="POST" action="/ro ...

Tips for utilizing the getServerSideProps function

I want to improve the SEO friendliness of my page by utilizing the getServerSideProps method. Despite searching online, I couldn't find a specific example that fits my situation. I am uncertain about how to proceed since I currently have a useEffect m ...

What methods are available for implementing hover effects within attributes using a JavaScript object?

const customPanelStyle = { 'background-color': 'red', 'border': '1px solid green', ':hover'{ 'background': 'blue' } } class some extends React.Component{ rende ...

jQuery ajax doesn't function properly on the server, it only works locally

When I send a jQuery Ajax request from my front-end to the back-end to retrieve values for calculations, it works perfectly on my local web server. However, when I try it online, all I get is a result of 0 in my calculations, indicating that the Ajax respo ...

Leveraging Vue's "v-slot" functionality to create a specified slot within a JavaScript object

I have turned to this platform seeking guidance on using "v-slot" while utilizing a syntax that involves a javascript object. This specific method was introduced in the online course Intro to Vue3 which I recently completed. Below is an image de ...

Vue Native Script refuses to install

Attempting to utilize vue-native, I encountered an issue while trying to install vue-native-scripts via npm. PS E:\Coding\VueNative\YAAdmin> npm i vue-native-scripts npm WARN optional SKIPPING OPTIONAL DEPENDENCY: <a href="/cdn-cgi/l ...

Alternative image loading in a figure element

I'm currently in the process of putting together an image gallery using Angular 4.3.0, where images are displayed as background images within figure elements rather than img tags. The images are initially resized to smaller dimensions before being use ...

Creating a personalized aggregation function in a MySQL query

Presenting the data in tabular format: id | module_id | rating 1 | 421 | 3 2 | 421 | 5 3. | 5321 | 4 4 | 5321 | 5 5 | 5321 | 4 6 | 641 | 2 7 | ...

Three.js ensures that the mesh texture does not stretch, instead it covers its container perfectly

I have a container where I apply an image using three.js and mesh. Here's how I add my mesh to the scene: this.$els = { el: el, image: el.querySelector('.ch__image') <-- size of container image is applied to }; this.loader = ne ...

Is it acceptable to compare a boolean with a string?

In my code, I have a variable called isRefreshed which is initially declared like this: var isRefreshed = ''; Sometimes, in certain scenarios, isRefreshed can be assigned a boolean value, for example: isRefreshed = false; Now, there is an if ...

Is there an HTML tag that can contain a block of JavaScript code without being processed by the browser?

I am dealing with the code below: <!-- Facebook Pixel Code --> <script> !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; n.push=n;n.loaded= ...

How to implement a for loop within a Vue.js method

Within Vuejs, I have a method that updates multiple variables based on user selection. methods: { updateChart(){ this.chart1.series[1].data = [this.$store.state.selectedcities[0].value[1]]; this.chart1.series[2].data = [this.$store.state.selecte ...

The utilization of the base64 function leads to an InvalidCharacterError

My goal is to implement the base-64 module method in my node.js + express project. Here is the code snippet: router.get('/list', function(req, res, next) { client.query('SELECT * FROM Document',function(err, row){ if(err) ...

Enhance your Vuejs Project by incorporating the Vuetify data table component

Looking to integrate the Vuetify Data Table into my current Vuejs project. After installing globally with vue add vuetify, I need assistance on how to successfully implement it. Any guidance is appreciated. Thank you. Thanks ...

Mastering asynchronous function handling in Node.js

I'm currently experiencing an issue with printing two statements using two functions var mongoose = require( 'mongoose' ); var_show_test = mongoose.model( 'test' ); exports.showTest = function(req,res) { var jsonString = []; ...

Adding a tooltip to a specific header in a Vue list - here's how!

My goal is to add a tooltip to a specific header labeled 'Retire' within a data table, without affecting any of the other headers. It's been quite the learning experience for me as a Vue novice, but with the help of AI (chatgpt), I've m ...