Difficulty with conflicting styles when dynamically loading components in Nuxt3

I'm facing a challenge with my Nuxt 3 application where I need to dynamically load different Header components for Mobile and Desktop devices.
I have created Header.vue and MobileHeader.vue for this purpose, and want to display them based on the device type.
Below is my layout code:

<template>
  <MobileHeader v-if="isMobile" />

  <Header v-else />
  
  <slot />
</template>
<script setup>
const MobileHeader = defineAsyncComponent(() => import('~/components/layout/MobileHeader.vue'));
const Header = defineAsyncComponent(() => import('~/components/layout/Header.vue'));
</script>

The Header.vue and MobileHeader.vue files both come with their individual SCSS stylesheets imported as shown below:

<style lang="scss">
@import "/assets/scss/components/layout/mobile-header";
</style>

The issue here is that both mobile-header.scss and header.scss files are being loaded on the page regardless of which component is displayed. I only want each file to be loaded when its respective component is rendered.

Any suggestions on how to resolve this?

Note: I've disabled component autoloading in my nuxt.config.ts
Also, I attempted to import components in the standard way but ran into issues.

import MobileHeader from '~/components/layout/MobileHeader.vue';

Answer №1

After spending some time investigating, I have finally found a solution to the issue at hand. To help others facing the same problem in the future, below is the updated code snippet that resolved the issue:

<template>
  <Header />
  <slot />
</template>
<script setup>

const {isTablet} = useDevice();

const Header = defineAsyncComponent(() => isTablet ? import('~/components/layout/TabletHeader.vue') : import('~/components/layout/Header.vue'));

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

Endless cycle of Facebook login prompts

Currently, I am utilizing the Facebook JavaScript SDK for a login button on my website. The functionality is working correctly, but there are two specific use cases where I seem to be encountering some issues. One issue arises when the Facebook cookie is ...

Problem with email verification process

Hey there, I'm currently working on validating an email address using regular expressions. Here is the code snippet I'm using: <input type="text" name="email" id="email"/> var email = $("input#email"), re = /^[A-Za-z ...

Obtain facial rotation using Three.js

In my code, I am calculating the intersections of mouse clicks with Three.js as follows: myVector.set( (event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5); myVector.unproject(myApp.camera); myRay.se ...

Guide to adding a Scrollable Navbar

Is the term "scroll bar" really accurate? I want to create a scrollable feature where users can easily select a city using their mouse wheel (or swipe on a smartphone) and then choose from possible cities within that country in a second window. Something s ...

Is there a way to verify if an ID includes more than one word?

I am trying to target a specific div with a unique id in jQuery: <div id="picture_contents_12356_title"></div> The '12356' is autogenerated and must be included in the id. I need to create a jQuery selector that combines "picture_co ...

When incorporating Vue as an npm package, a Vue 3 app may inadvertently clear the mounted element upon initialization

I have a straightforward Vue 3 application that is working perfectly fine when I include Vue as a script, as shown in the code snippet below. However, I need to integrate it with webpack by including it as an npm package. When I do this, the app loads but ...

Images are no longer accessible from the public directory once a certain route is reached

My app has default layouts (footer and header) set for all pages: page.default.layout = name.startsWith("Dashboard/") ? undefined : MainLayout; The footer uses an image from the public directory, public/images/ <img src="images/payment.png" class="img- ...

The script fails to execute on content loaded through AJAX in Django

I have a website with nested div elements that make up a complete set. These elements can be dynamically loaded when the user clicks the "load more" button. The website includes a script that changes the style of the aforementioned div element when the pa ...

Utilizing external libraries with RiotJS for greater functionality

My jQuery flexslider is causing slide animation issues because the library is being loaded before the DOM. As a result, I can't trigger actions of the flexslider. Here's the HTML structure: <html> <body> <home-templat ...

Retrieve specific components of objects using a GET request

When visitors land on my web app's homepage, a GET request is triggered to fetch a current list of Stadiums stored in the database through my API. However, the Stadium objects retrieved are packed with unnecessary data, particularly extensive arrays o ...

Fading text that gradually vanishes depending on the viewport width... with ellipses!

I currently have a two-item unordered list positioned absolutely to the top right of the viewport. <header id="top-bar"> <ul> <li> <a href="#">David Bowie</a> </li> <li> ...

Implementation of Material UI Autocomplete feature with options consisting of an array of objects linking to both ID and label properties

Utilizing the Material UI Autocomplete component in my project has been a key feature. Referencing the official documentation, here are the available options: let options = [ { id: "507f191e810c19729de860ea", label: "London" }, { id: "u07f1u1e810c19 ...

What is the most effective method for attaching a jQuery click event to every anchor tag within each row of a table?

Displayed here is a grid (basic html table) showcasing users with the option to delete a specific user by clicking on the delete link. My typical approach involves: <% foreach (var user in Model.Users) {%> <tr > <td align="right"><% ...

Utilizing JavaScript files within Angular2 components: A guide

I need to insert a widget that runs on load. Typically, in a regular HTML page, I would include the script: <script src="rectangleDrawing.js"></script> Then, I would add a div as a placeholder: <div name="rectangle></div> The is ...

Use JavaScript to load and set a background image for a div

When it comes to loading different images onto an "img" tag by printing their URLs using JavaScript and then letting CSS manipulate the content in the tag, I have a code snippet that does just that. $(window).load(function() { var randomImages = [&apo ...

I attempted to implement dialog functionality using material-ui code from the documentation, but for some reason it's not functioning correctly. Can anyone point out what I might

I tried implementing a material-ui dialog feature for React, but I'm facing issues with it. When clicking on the contact button, the handleClickOpen method is not being triggered at all. The contact button is supposed to open the dialog box, and all ...

The HTML drag and drop API is causing an issue where the `getData`

My website features a drag-and-drop functionality, and I'm utilizing the HTML drag-and-drop API to implement it. However, when I attempt to drag and drop an SVG image onto the canvas, it displays as undefined, and I would like the image to remain in t ...

Learn how to display JSON data sent from the server on a webpage

I'm just starting out with jquery. I have an api called '/categories' that gives me a json object of categories. The response looks like this: [ { name: "Laptop deals", slug: "laptop-deals", imageURL: "image.jpg", id: 1 }, { name: "Fashion ...

Dynamically display or conceal a b-table column depending on store getters

I need to dynamically display or hide the table column 'first_name' based on whether the user is an isAdmin. Below is the structure of my table: <b-table striped hover small stacked="sm" selectable select- ...

The error message "app.use() function requires middleware function" appears when the app

Currently, I am in the process of learning node.js with express template engine by following a Udemy course called "Learn Node.js by Building 10 Projects". During one of the lectures, when the professor ran npm start localhost:3000, it worked fine for him, ...