Troubleshooting Angular 2 Fallback Route Failure

My current project is using Angular 2 Webpack Starter but I am having trouble with the fallback route. In my app.routes.ts file, I have defined the routes as follows:

import { Routes } from '@angular/router';
import { HomeComponent } from './home';
import { DataResolver } from './app.resolver';

export const ROUTES: Routes = [
  {
     path: '',
     component: HomeComponent
  },
  {
     path: 'getstarted', loadChildren: './getstarted#GetStartedModule'
  },

  ...
  {
     path: 'notfound', loadChildren: './notfound#NotFoundModule'
  },
  {
     path: '**', loadChildren: './notfound#NotFoundModule'
  },
];

The 'not found' path is working fine, but the fallback route (**) is not behaving correctly. Instead of loading the NotFoundModule, it does not load any module and no errors are shown. However, if I change it to redirect like this, it works as expected:

  ...
  {
     path: 'notfound', loadChildren: './notfound#NotFoundModule'
  },
  {
    path: '**', redirectTo:'/notfound', pathMatch: 'full'
  },
];

I do not want to use redirection because I do not want to alter the URL to /notfound. How can I get the original setup to work or what other options do I have to resolve this issue?

Answer №1

After giving it a go, I've discovered that lazy routes cannot be used to assign your fallback page. A workaround for this issue would be:

export const ROUTES: Routes = [
  {
     path: '',
     component: HomeComponent
  },
  {
     path: 'getstarted', loadChildren: './getstarted#GetStartedModule'
  },

  ...
  {
     path: 'notfound', loadChildren: './notfound#NotFoundModule'
  },
  {
     path: '**', component : NotFoundComponent
  },
];

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

Using Typescript to retrieve the Return Type of a function when called with specific Parameter types

My goal is to create 2 interfaces for accessing the database: dao can be used by both admins and regular users, so each function needs an isAdmin:boolean parameter (e.g. updateUser(isAdmin: boolean, returnUser)) daoAsAdmin, on the other hand, allows metho ...

"Angular encountered an error while attempting to access the property 'data' from an undefined

I'm encountering an issue when trying to retrieve data from a JSON file. Upon clicking a button to display the data in a textarea, the first click does not yield any result, but subsequent clicks work as expected. The program functions by fetching an ...

Angular: bypassSecurityTrustHtml sanitizer does not render the attribute (click)

I'm facing an issue where a button I rendered is not executing the alertWindow function when clicked. Can anyone help?: app.component.ts: import { Component, ElementRef, OnInit, ViewEncapsulation } from '@angular/core'; import ...

The plugin's element is not compatible with jQuery methods

This particular plugin is designed to enhance the appearance of checkbox inputs. It creates a more visually appealing version of standard checkboxes. However, I have encountered an issue with one of the variables in the code. Let's discuss the theLab ...

Alter the value of an input element using JavaScript

There are multiple hidden input fields on the page I'm currently working on: <input a1="2" a2="1" a3="3" name="Value" type="hidden" value="10"> <input a1="4" a2="2" a3="6" name="Value" type="hidden" value="12"> <input a1="6" a2="3" a3 ...

ng-repeat not displaying any content

I am trying to create a form where users can input extra information by adding new rows, but I am struggling with generating the first row <div ng-repeat="row in rows"> <input type="text" placeholder="name"><input type="tel" placeholder="te ...

What is the best way to format the information when using response.send() in express.js?

I need help with customizing the content I'm returning in the app.post() method. Is there a way to do this? Below is an example of the app.post() code: app.post("/",function(req,res){ const query = req.body.cityName; const cityName = query.charA ...

Is it possible to capture and store server responses on the client side using Node.js and React Native?

Here's a POST request I have: router.post("/projects", async (req, res) => { const { projectName, projectDescription, projectBudget, projectDuration, industry, companyName, numberOfEmployees, diamond, } = req.bod ...

Tips for showing a single progress message when uploading multiple files on eleme.io [vuejs]

Tech Stack: Utilizing Vuejs with element.eleme.io framework. Objective: To implement a feature that allows users to upload multiple files while displaying only one "in progress message". To handle image uploads, we are integrating . During the upload pr ...

Discover a method to receive an alert when the mouse exits the inner window along the y-axis

Is there a way to receive an alert if the mouse moves out of the inner window solely in the y-axis? Currently, alerts are triggered when the mouse moves out on both x-axis and y-axis. For example, if the mouse pointer hovers over the address bar coming fro ...

Uploading Files through Reactive Forms in Angular

I tried following a tutorial on integrating file upload functionality into my reactive form, which can be found at the following link: . However, I've encountered an issue where I'm getting an error message stating "this.onChange is not a functio ...

What specific type should be used for validations when incorporating express-validator imperative validations?

Having trouble implementing express-validator's imperative validations in TypeScript because the type for validations cannot be found. // reusable function for multiple routes const validate = validations => { return async (req, res, next) => ...

When a service alias is provided in Angular 7, it disrupts the build execution process

After creating a service called MyService and its mocked version - MyServiceMock, I utilized the mock service in my unit tests until the backend is ready, providing and using the results from the mocked service. To avoid future code changes when the backe ...

Having trouble getting JavaScript to select a stylesheet based on time?

I am attempting to implement two different styles based on the time of day. Here is the code I am using to select the CSS file depending on the current time: <script type="text/javascript"> function setTimedStylesheet() { var currentTim ...

Encountering issues with integrating interactjs 1.7.2 into Angular 8 renderings

Currently facing challenges with importing interactive.js 1.7.2 in Angular 8. I attempted the following installation: npm install interactjs@next I tried various ways to import it, but none seemed to work: import * as interact from 'interactjs'; ...

Nextjs API call ended without a response being sent

I am currently facing a challenge in my NextJS project as my endpoint API does not support multiple calls, and I am looking to implement a data refresh every 3 minutes from the original source. To achieve this, I have integrated an API in NextJS by creati ...

Vue JS: Extracting both the unique ID and value from an array of input simultaneously

I am new to Vue and currently exploring its capabilities. I am experimenting with the Element UI for Vue's user interface. Specifically, I am working with the Input Number Component, to manage an array of data. Let's assume my data is structured ...

Retrieving package information from NPM using a web browser

I am currently developing a Chrome Web App that retrieves information from NPM. However, I have encountered issues due to Chrome adhering to the Access-Control-Allow-Origin flags set by websites. While I am able to access the following URL: http://regist ...

Unlocking the Power of Localization: An Easy Guide to Accessing JavaScript Values

After utilizing the localization helper created by Matt Hawley, I found it to be incredibly effective. However, I am encountering an issue when attempting to retrieve the values in javascript/jQuery. For example, I am unable to fetch the resource text usi ...

Incorporate a customizable month option within a flexible calendar design

I'm working on creating a calendar that adjusts to different screen sizes for my website. The script I've implemented is as follows: <script type="text/javascript"> $(document).ready(function () { $(".responsive-calendar").responsiv ...