ERROR: Unhandled promise rejection: Unable to find a matching route for URL Segment 'main/knowledge-base'

After setting up dynamic routing for my Angular 6 application, I encountered an error when clicking on a link (for example, 'knowledge base') that stated:

core.js:1673 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'main/knowledge-base'

I referred to tutorials Here and Here for implementing dynamic routing in my application to enable components to create links, display them, and redirect when clicked. To test this, I created a dummyComponent that each route initially uses, to be replaced by actual components once it's functional.

Despite following these resources, the error prevented progress. I looked into possible solutions like this and this, but they didn't resolve my issue. Is there something I overlooked?

Below is the code snippet:

toolbar.component.ts

// Code snippet for toolbar.component.ts

toolbar.component.html

// Code snippet for toolbar.component.html

dynamic-routing.service.ts

// Code snippet for dynamic-routing.service.ts

app.routing.ts

// Code snippet for app.routing.ts

Expected: Links should redirect to routes like "main/name"

Actual: Error message displayed upon clicking on a link.

Answer №1

The issue you are facing is that you have not defined any child route paths for /main. This is why you are encountering that error. Based on your current setup, you can only route to /main. To resolve this, consider the following:

You have two options:

Option 1

export const routes: Routes = [
  { path: 'main',
    component: MainComponent,
    canActivate: [AuthGuardService],
    pathMatch : 'full'
  },
  { path: 'main/:somePath',
    component: MainComponent
  },
  {
    path: '',
    component: AppComponent,
  }
];

Option 2:

export const routes: Routes = [
  { path: 'main',
    children :[
       {path: ':somePath', component: MainComponent}
    ],
    component: SomeComponent
  },
  {
    path: '',
    component: AppComponent,
  },
];

NOTE: It is recommended to always add an empty route path at the end. This allows for more specific routes to be matched first. Otherwise, you may need to use pathMatch:'full'.

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

Creating a navigational sidebar with dynamic responses using Angular 8

Currently, I am working on creating a responsive sidenav USING ANGULAR WITHOUT MATERIAL DESIGN. My goal is to achieve the same level of responsiveness that can be seen on https://angular.io. Within my project, I have created separate components for the top ...

Creating a single definition that encompasses the characteristics of both primitive and generic types without the need for combining them

Here is a scenario where I need to consolidate and refactor two types: ... .then((result1: any) => { let promises = { one: $q.when(val1), two: $q.when(val2) }; return $q.all(promises); }) .then((resolvedPromises: any) => { ...

Is there a way to establish a boundary for the forEach function in TypeScript?

In my web-based game, I use the forEach command to retrieve the team players in the lobby. However, for a specific feature in my game, I need to extract the id of the first player from the opposing team. How can I modify my current code to achieve this? T ...

Why does my Angular CLI default to generating CSS files instead of SCSS?

Before transitioning to Angular 9, my project was set up to use SCSS when running the ng generate command, and it worked perfectly. To my disappointment, when I created my first component using ng g c in Angular 9, it generated a css file instead of a scs ...

angular2 ngFor is not functioning properly

I'm having an issue where I cannot get textboxes to appear whenever a user clicks a button. I am attempting to achieve this using ngFor, but for some reason, the ngFor is not iterating as expected. Even after trying to change the array reference with ...

Create a CRUD interface in Angular 5 using automated generation techniques

Is there a framework available that can assist in creating CRUD view files from a database table within an MVC project, with Angular as the front-end? I have spent hours searching on Google but still haven't found a solution. All I came across were i ...

Hello everyone, I'm encountering problems while trying to retrieve data from

I've been experimenting with the ngrx store, but I'm encountering some difficulties: Mainly, I'm having trouble fetching information from the store. You can find my code here: https://github.com/FabioBiao/ngrxtTest In the home.component.t ...

Displaying related objects information from a single object in AngularFire2 can be achieved by following these steps

As a newcomer to Angular and Firebase, I apologize if my question seems basic. I'm seeking guidance on how to display related object information from one object in angularfire2. Specifically, I want to show the role names assigned to a user. Here is ...

The parameter type '{ email: string; }' in NGXS does not accept arguments of type 'string'

Struggling to retrieve data from an API using ngxs with this.store.dispatch(new GetUser(userEmail)) Attempted to use the user id stored in local storage as a string and convert it to a number but encountered a similar error (Argument of type 'string&a ...

Using Vue.js 2 on multiple HTML pages with Typescript and ASP.Net Core

My ASP.Net Core MVC project utilizes VueJs2 for more complex tasks, with each view having its own corresponding js file. The directory structure is as follows: ├ Controllers\HomeController.cs (with actions Index & Details) ├ Scripts\Hom ...

What is the TypeScript declaration for the built-in 'net' NodeJS types?

I'm currently working on developing a TCP client application and it seems like the 'net' package in NodeJs will be perfect for what I need. However, I've run into an issue finding the TypeScript definitions for this package. If you hav ...

Safari does not display disabled input fields correctly

I have designed a simple angular-material form with various inputs that are organized using angular flex-layout. The form displays correctly in all browsers except for Safari on iOS devices. The problem in Safari arises when loading a form that contains d ...

Typescript: The property 'userId' is not found within the 'unknown' type

For my rxJS practice in Typescript, I wrote the following simple code. import fetch from 'node-fetch'; import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent, SubscriptionLike, PartialObserver } from &apo ...

Previewing images with Dropzone through extending file types

Want to display an image preview before uploading using dropzone. Attempting to access the images by calling file.preview but encountering the error "it does not exist on type File." Dropzone extends the file type with a preview?: string. How can I access ...

What is the process for passing information to a nested component structure with parent-child-child relationships?

I am facing an issue with three nested components (C1, C2, C3) where C2 is called within C1 and C3 is called within C2. My goal is to pass data from C1 to C3 using property binding. In the template of C1, I successfully bound a variable that I can access ...

Varied Data Transfer Objects used for requests and responses

Currently, I am dealing with MongoDB and subdocuments. MongoDB automatically adds extra fields that cannot be specified in a POST request; they can only be retrieved using a GET request. To put it simply: different data transfer objects (dtos). I am utili ...

Error in Ionic2 TypeScript: 'google' and '$' names not found, despite Google Maps and jQuery functioning correctly

I am currently working on developing an ionic2 application using TypeScript. Within the index.html file, I have attempted to integrate jquery and the Google Maps JS API before cordova.js: <!-- Vendor --> <script src="https://maps.googleapis. ...

An issue has occurred with the NullInjector, specifically regarding the AppModule and Storage in Ionic 4

When attempting to launch my Ionic app using npm start, an error message appears stating "NullInjectorError: No provider for Storage!". I have already included Storage in the app.module.ts file as shown below: imports: \[ BrowserModule, IonicModule ...

Encountering problem while upgrading Angular Material from version 13 to 14 - error TS2307: Module '@angular/material/core/common-behaviors/constructor' not found

While upgrading Angular and Angular Material from version 13.2.6 to 14.2.2, I encountered the following challenges: Error TS2307: Module '@angular/material/core/common-behaviors/constructor' or its type declarations cannot be found. Error TS2345 ...

When switching tabs, Ion-select should not reload the selected name

Whenever I switch tabs and then return to the previous tab in Ionic, the select field that was previously set becomes null, even though the page is still loading and the variable is populated. <ion-header color="primary"> <ion-navbar> &l ...