I am having trouble with CSS, JS, and image files not loading when using Angular 9 loadChildren

I am facing an issue with loading CSS, JS, and images in my Angular 9 project. I have separate 'admin' and 'catalog' folders where I want to load different components. However, the assets are not loading properly in the 'catalog' folder as shown in the image below:

https://i.sstatic.net/vOGe2.jpg

Here is my routing configuration in app-routing.module.ts:

 // Routing configuration code here 

In catelog.module.ts:

// Module imports and declarations

In catelog-routing.module.ts:

// Routing configuration for catalog module

Contents of app\catelog\pages\common\common.component.html:

// HTML code for common component

My folder structure:

https://i.sstatic.net/8bB3I.jpg

I am able to load CSS, JS, and images properly in the 'admin' folder, but facing issues with the 'catalog' folder. Can someone please assist me in resolving this problem?

Please provide guidance to fix this loading issue.

Answer №1

A better practice is to avoid putting entire

<html><body>...</body>
within your component template. Instead, the common.component.html should be structured like this:

<header>someHeader</header>
<router-outlet></router-outlet>
<footer>some data</footer>

This means excluding elements such as

<html>, <body>, <srcipts>
, etc. If you require bootstrap.css, it's recommended to load it in the global styles.scss file. For loading JS files, utilize angular.json (there is a section dedicated for additional JS scripts).

Answer №2

To solve the css issue you are facing, consider implementing the following project route structure:

Begin by structuring your project routes as follows:

<html>
  <body>
     <header-comp/>
      <app-comp></app-comp>
     <footer-comp/>
   </body>
 </html>

In the app component html file, include the router outlet:

<router-outlet/>

In the app component module, add routes to two lazy loading modules (home and admin) along with an extra route entry that redirects the default route to the home module.

Each module (home and admin) will have its own homepage, serving as the default entry point of the routing module with sub children routes to other pages. Here is an example of what the html for that page might look like:

<router-outlet></router-outlet>

In the module's homepage component definition, make sure to specify the desired bootstrap css version:

@component({
 selector:'home-app-comp',
 html:'<router-outlet/>',
 styleUrl:[path to css file]
 })

This approach emphasizes nested routes, where each module's main page loads a specific bootstrap version and serves as a parent container for the rest of the module's pages.

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

Prevent unnecessary requests for asset images in Angular 5

Within my Angular application (running version 5.1.0, built with angular-cli and webpack), I have a country selector component that allows users to choose a country from a drop-down menu or by typing the name in an autocomplete field. Each matching result ...

Configuring React classes for compilation within Play

After incorporating a basic React class into my Play project at /app/assets/js: var Question = React.createClass({ render: function() { return (<div> <p>{this.props.content}</p> <br> <p>{this.props.an ...

A Simple Guide to Setting a Background Image in React Native with the Nativebase.io Library

What is the process for including a background image in React Native with the help of the Nativebase.io Library? I have a specific screen where I need to incorporate a background image, with all other elements positioned at the center of the image. ...

Remove the class from all child elements and apply the class to the parent or ancestor of the element that was clicked on using

The Goal: I aim to achieve the following outcome. Whenever a user clicks on any of the radio inputs within the div that has the class corp-struct, I want to assign the following class to the parent element two levels up. This means that, in the example bel ...

What is the best way to create Jest unit tests for an Angular Component without using TestBed?

I'm diving into the world of Jest testing and feeling lost as to why my tests keep failing. If you have any recommendations for videos or articles that focus on writing Jest unit tests for Angular components and services without using "TestBed," plea ...

Error 404: Angular version 18 - Request not found

After upgrading my Angular application to version 18 (18.0.1), I've encountered issues with http requests on localhost. The majority of my requests are returning a 404 error. I am operating behind a corporate proxy and have a configuration file in pla ...

Determine the aspect ratio of the image and incorporate it into the query string

In my current code, I am extracting the dimensions of an image obtained from a cropper tool. I then calculate the aspect ratio of the image by dividing the height by the width. This value is then incorporated into the query string url: urlLocation + imag ...

PrestaShop - Using ajax to update a JSON object with PUT request

I've been attempting to update a JSON object (such as a customer), but I keep encountering the following error: "NetworkError: 405 Method Not Allowed - ..." Here's my code (index.js): var testWebService = angular.module('testWebService& ...

Sending documents to a printer directly from a Rails application

Is there a library or gem in Rails that can be used to print the contents of a web page directly onto paper? I am curious if it's possible to specify only a specific part of the page, such as a div, for printing. Any guidance, advice, or tutorial link ...

Send a Symfony form between different views using ajax

I am currently working on creating a tabs navigation system with a central block that displays a Symfony form. Whenever I click on a tab link, I want to reload the block with the form and data related to that tab. The challenge I am facing is how to pass ...

Steps for opening standalone angular2 and TypeScript project in visual studio: A guide to launching your project

What is the process for accessing an Angular2 and TypeScript project in Visual Studio without needing npm or Node.js? I require the ability to open the project on a computer that is not connected to a network. Many thanks ...

What is the best way to insert an HTML attribute into a dynamically generated build script within Angular?

After running the ng build --prod command, Angular creates 4 scripts. One of these is called main.js. I am wondering if there is a way to dynamically add an HTML attribute to this script tag in the index.html file once the build process is complete. I nee ...

Transform PHP array into a properly structured array or object that can be easily used in JavaScript

My PHP code contains an array that looks like this: $variation = [ attribute_label => "Choose your Color", attribute_name => "pa_choose-your-color", variations => [ "819" => "Red", "820" => "Blue", ...

Managing Errors in Angular: Utilizing Interceptors and Modal Pop-ups

I recently finished developing an Angular 5 application that effectively handles errors for each API call made. By utilizing the HttpClient, I am able to catch errors that occur post request sent to the server. The error interception occurs within a servic ...

Challenge in backward compatibility when converting from .on() to .live()

Struggling to make hammer.js work with an outdated 1.6 jQuery in my CMS. The function "on()" isn't available, so I have to use "live()". Here are the two instances: 1. var hammertime = new Hammer(element[0], { drag_lock_to_axis: true }); hammertime. ...

Using Conditions in AngularJS: Choosing Between Callbacks and Promises in a Service

I am currently faced with a specific scenario where I am uncertain whether to implement a callback or a promise. As someone who is relatively new to promises and just beginning to grasp their concept, I want to avoid falling into any potential anti pattern ...

The div that scrolls gracefully within its boundaries

Currently, I am working on a task that involves a div containing images that need to be scrolled left and right. I have successfully implemented the scrolling functionality using jQuery. However, I now face the challenge of ensuring that the content stays ...

Convert a binary array into a signed 64-bit integer

Although JavaScript may not accurately represent all 64-bit integer numbers, it can handle numbers larger than 32 bits, which is what I am looking for. I need JavaScript to provide me with whatever level of precision it can offer. I have a byte array of a ...

Service Registry with TypeScript type safety

Is there a way to implement a type-safe service registry in Typescript? I want a function that can return a specific type based on the argument provided. For example: get("a") should return an object of type ServiceA. get("b") should re ...

What is the process for creating a symbolic link from a project's node_modules directory?

Recently, I've been learning how to build a Password Manager using React, Node.js, and MYSQL by following a tutorial. However, I encountered an issue where the file /EncryptionHandler was located outside of the src/ directory of my project. Even thoug ...