The complete page gets re-rendered when Nuxt child routes are used

When I attempt to utilize child routes, my goal is to maintain specific data on the page while modifying other content.

To illustrate this concept, I have created a straightforward example available at this link.

After selecting "cat" and increasing the counter multiple times, you will notice that switching to "dog" results in the counter resetting. However, I am seeking a solution where the counter retains its count.

I am wondering if I am misinterpreting the intended functionality of <nuxt-child>. Any insights would be greatly appreciated!

Answer №1

Each section of your website, such as the cat page (/cat) and dog page (/dog), has its own unique counter that operates independently from one another. To ensure that the counter remains active, it is recommended to incorporate it into the layout structure.

One approach could be moving the counter functionality into a separate component, like ButtonCounter.vue, which can then be utilized within the layout:

// components/ButtonCounter.vue
<template>
  <div>
    <div>
      <button @click="counter++">increase</button>
      <p>button clicked {{ counter }} times</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      counter: 0,
    };
  },
};
</script>
// layout/default.vue
<template>
  <div class="container">
    <ul style="padding: 20px; border: 1px solid black">
      <li><nuxt-link to="/cat">cat</nuxt-link></li>
      <li><nuxt-link to="/dog">dog</nuxt-link></li>
    </ul>
    <div>
      <ButtonCounter />
      <Nuxt />
    </div>
  </div>
</template>

Check out the demo here!

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

Guide on incorporating Bootstrap JS into HTML5 reusable web elements

RESOLVED: the solution is in a comment TL;DR: Issues triggering Bootstrap's JS, likely due to incorrect import of JS scripts I've been working on integrating Bootstrap with my custom reusable web components across all pages. Specifically, I&apo ...

Error came up as "backbone.radio.js" and it threw Uncaught SyntaxError since the token import appeared unexpectedly

I've been struggling to transition an application from Backbone to Marionette (v3) for the past two days. Every time I try to run the app in the browser, I keep encountering this error in the console (resulting in a blank screen): Uncaught SyntaxErr ...

Using an image as the background for a three.js scene may cause it to appear distorted

I am struggling with setting an image as a background for my scene without it becoming blurry. I have attempted to use the following code: textureLoader.load("images/background/space.png", function(t) { scene.background = t; }); The problem arises when ...

What sets apart posting data through an HTML form submission from posting data through an Ajax request?

Recently, I've encountered an issue with my Post API. When calling it through AJAX, the user parameter is received but the StreamReader returns empty. [HttpPost] [Route("getUserBankList")] public IHttpActionResult getUserBankList(UserProfile ...

How to dynamically adjust font size using Javascript

Is there a way to adjust the font size of the left-column using JavaScript? <div id="left-column"> <a href="">1</a><br> <a href="">2</a><br> <a href="">3</a><br> <a href=""> ...

acquiring specific array elements in Vue.js

Currently, I am integrating Vue.js in the frontend of a Laravel application and need to retrieve values from an array column and store them in a new array. Is there a method similar to PHP's array_column in Vue.js that can accomplish this task? ...

reactjs implementation of a virtualized list

Recently, I stumbled upon the React-window library and found it to be incredibly useful. Intrigued by how it was built, I decided to try my hand at creating my own virtualized list. However, I encountered an issue where the list could only scroll up to it ...

Configuring environment variables in Vue CLI

When using webpack, I found a way to set environment variables in my package.json as shown below: "scripts": { "dev:visualise": "webpack serve --open --config webpack.dev.js --env entry=visualise", "dev:order": & ...

Navigating a path and executing unique functions based on varying URLs: A guide

I am trying to send a post request to the path /users and then right away send another post request to /users/:id. However, I need the actions to be different for each of these URLs, so I cannot use the array method to apply the same middleware. The goal ...

Why are the indicators and controls for Twitter-Bootstrap 5 carousel not functioning properly?

Currently, I am working on developing an ecommerce website using the Django framework. However, I am facing challenges with making my bootstrap carousel function properly. When trying to interact with the indicators and controls, nothing happens as expect ...

"What is the best way to include additional fields within a popover in an AngularJS application

This is my code for implementing a popover using AngularJS. I am trying to figure out how to add custom styling to the popover HTML, but I am having trouble binding elements in that part of the code. <!DOCTYPE html> <html> <head> <l ...

Interference in the output of .innerHTML leads to disruption

I have been working on a feature to display a calculated price based on the selected quantity for each individual product. I attempted to duplicate the code and modify variable names, but encountered issues with the increase/decrease buttons triggering mul ...

Can CKEditor be integrated with Mutation Observer? If so, how can this be achieved?

Trying to detect changes in the current CKEditor content. The goal is to identify which element's content has been modified when a user writes multiple paragraphs. Not well-versed in JavaScript or jQuery, but managed to come up with this code after s ...

JavaScript JQuery alert popup fails to display

I have been attempting to create an alert pop-up using JQuery that will display when a user clicks on submit without filling out the form. However, when I test my code by leaving the form empty and clicking 'submit', the page just refreshes with ...

Injecting arbitrary text following ?= in a web URL

Consider the following code snippet for a page named MyWebsite.com/page.php <?php $username = "SuperUsername"; $password = "SuperPassword"; if (isset($_GET['p']) && $_GET['p'] == "login") { if ($_POST['user&ap ...

A guide to organizing the alphabet into groups and displaying them in a Vue template

In my Vue.js data, the structure looks like this: new Vue({ el: "#app", data() { return { alpha: [{ artist: "Aa" }, { artist: "Az" }, { artist: "Ab" }, { artist ...

Warning in Google Script editor

Currently, I am working on creating some quick scripts to manipulate spreadsheets in my Google Drive. However, I am cautious about the script unintentionally running and making changes to data before I am ready or executing multiple times after completing ...

The element div is not permitted as a child of the element h5 in this particular scenario

My code snippet is as follows: $compile .= "<h5 data-count='".$acctemmpi. "' class='shortcode_accordion_item_title expanded_". $expanded_state . "'>" . $title . "<div class='ico'&g ...

The Loopback access control list (ACL) for the admin role is failing to be

Seeking assistance with troubleshooting why my 'admin' role is not functioning in loopback 3.x. Here are the codes I am using: script.js - Contains code for creating admin roles in a postgres database. module.exports = function (app) { var User ...

Incorrect behavior of responsive background images

For the first time, I am working on a school project that requires me to create a responsive website. I am following the "mobile first" approach. On stackoverflow, I came across a helpful way of making background images responsive: background-image: url(. ...