The NuxtLink feature in Nuxt3 doesn't automatically refresh the layout CSS when the route changes

The functionality described in the code snippet below is quite straightforward. When a user visits the /blog route, the layout is switched to the blog layout which has a different header style. Similarly, the homepage has its own unique header style. However, when using NuxtLink, the page layout styles do not update automatically unless the page is manually refreshed each time.

There are two distinct layouts: default.vue

<template>
  <AppHeader />
  <slot />
</template>

<style>
.header-wrapper {
  background: red;
}
</style>

and the other layout blog.vue

<template>
  <AppHeader />
  <slot />
</template>

<style>
.header-wrapper {
  background: blue;
}
</style>

Within the pages directory, there exist two pages: index.vue

<script setup>
definePageMeta({
  layout: 'default'
})
</script>

<template>
  <div>
    <h1>this is the homepage</h1>
    <NuxtLink to="/blog">Blog</NuxtLink>
  </div>
</template>

and blog.vue

<script setup>
definePageMeta({
  layout: 'blog'
})
</script>

<template>
  <div>
    <h1>this is the blog page</h1>
    <NuxtLink to="/">Homepage</NuxtLink>
  </div>
</template>

Answer №1

Ensure that the styles are scoped to maintain uniqueness for each layout:

<template>
  <AppHeader />
  <slot />
</template>

<style scoped>
.header-wrapper {
  background: pink;
}
</style>

also,

<template>
  <AppHeader />
  <slot />
</template>

<style scoped>
.header-wrapper {
  background: green;
}
</style>

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

Achieve seamless integration of PHP function execution with Javascript onClick without the need for page

After delving into my research, it seems like Ajax and/or JQuery may be the way to go for what I'm trying to achieve. However, I wanted to double-check before moving forward in that direction. The task at hand involves using a javascript onclick func ...

Ways to detect if a script-blocking ad blocker like Ghostery is preventing a file from loading

I am currently working on a method to detect if ghostery is preventing google doubleclick ad scripts from being loaded. I prefer not to use a listener and instead want a straightforward way to determine if the script or URL is being blocked. Although the c ...

Can you please explain the significance of the plus sign in the expression +a[i]?

While analyzing a JavaScript code snippet for pagination, I came across the following line: if (+a[i].innerHTML === Pagination.page) a[i].className = 'current'; I am intrigued by the use of the + symbol in +a[i]. Here is the rest of the related ...

Storybook/vue causing issues with aliases not functioning as intended

I'm currently utilizing Storybook for Vue and I am endeavoring to integrate aliases into the webpack config within storybook/main.js: resolve: { alias: { 'vue$': 'vue/dist/vue.esm.js', '@': path.resolve ...

Which is the better option for opening a link in a button: using onclick or href?

What is the most effective way to open a link using a button? <button type="button" onclick="location='permalink.php'">Permalink</button> <button type="button" href="index.php">Permalink</button> ...

Loading a GLTF model directly from a file input with ThreeJS

When using ThreeJS, I want to allow the user to load any GLTF model they choose to display. To achieve this, I have implemented a file input tag where the user can click and select a gltf-file to upload from their file browser: <input id="selectedM ...

Unable to obtain the selection from the dropdown menu

I am currently populating data from a JSON file into a dropdown list. The code snippet I am using for this task is shown below: $(function() { $('#productList').append($('<option/>').attr("value", key).text(data[k ...

How can I combine three dynamic dropdown menus into a single dynamic multi-level dropdown using PHP and Javascript?

Hello everyone, I've created a three-level dynamic dropdown menu that includes categories, subcategories, and questions. Each category has a corresponding subcategory, and each subcategory has corresponding questions. Below is the code for this functi ...

Enhancing JavaScript Arrays by incorporating data from a JSON file

Could you kindly advise me on what I might be doing incorrectly here? It seems like a simple issue, but it has taken up the entire day. All I wanted to do was add a value to an array called messages from a JSON file. function get_message(params) { va ...

How to create a gelatin-like effect on a rectangle using HTML5 canvas

Currently, I'm working on a platformer game project in JavaScript. The concept involves players as rectangles jumping on and off platforms. While the basic functionality is set up, I'm now aiming to incorporate a unique 'gelatine effect&apos ...

When attempting to utilize yarn link, I receive an error message indicating that the other folder is not recognized as a module

After successfully running "yarn link," I encountered an issue when attempting to use it in a different project. The error message displayed was ../../.tsx is not a module. What could be causing this problem? const App: React.FC = () => { const [op ...

Vue - Checkbox for selecting all items

As I am still learning Vue, please bear with me as I attempt to solve this issue. I currently have a v-for loop that generates a list of checkboxes. Each checkbox works independently when clicked. However, my goal, as the title suggests, is to have a sele ...

The validation process in reactive forms is experiencing some issues with efficiency

Trying to debug an issue with my reactive forms - the repeatPassword field doesn't update as expected. When entering information in the "password" field, then the "repeatPassword" field, and back to "password", the second entry is not flagged as inval ...

Attempting to toggle the visibility of div elements through user interaction

I'm having an issue with click events on several elements. Each element's click event is supposed to reveal a specific div related to it, but the hidden divs are not appearing when I click on the elements. Any help in figuring out what might be g ...

Importing GeoJSON data into Meteor's Leaflet

Recently diving into Meteor, I am on a mission to create my own customized version of this impressive example from leaflet incorporated into Meteor: Interactive Choropleth Map The implementation requires the use of this GeoJson Data file: us-states The o ...

Modify the -webkit-text-stroke- hue using JavaScript

I've been working on a website that features text with "text stroke", and I am attempting to dynamically change the color of this text stroke using JavaScript. My goal is to have four different color options available. However, as it is based on webk ...

Experiencing excessive delay in loading data into the DOM following a successful response from a service in AngularJS

I am facing a challenge in loading a large set of data into an HTML table. To begin, you need to click on a button: <a ng-click="get_product()">load data</a> This button triggers a function called get_product: $scope.get_product = func ...

Tips for encoding AngularJS ng-model in real-time

Recently, I embarked on a new project and wanted to incorporate some AngularJS magic. The only obstacle is my limited expertise in AngularJS or JavaScript, making it difficult for me to figure out how to make it work. One key requirement is that the functi ...

Switching the cursor to an image when hovering over an element is causing inconsistency in hover events triggering

Currently, I am attempting to implement an effect that changes the cursor to an image when hovering over a text element and reverts back to the normal cursor upon leaving the text element. However, this functionality is not working as expected when using R ...

ClickAwayListener is preventing the onClick event from being fired within a component that is nested

I am encountering an issue with the clickAwayListener feature of material-ui. It seems to be disabling the onClick event in one of the buttons on a nested component. Upon removing the ClickAwayListener, everything functions as expected. However, with it e ...