Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: ''"

i am learning angular 2. so i want to render multiple html pages to make single page application. so i made it. but i got problem with routing. it is showing me error like this

 EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: ''
platform-browser.umd.js:1900 EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: ''BrowserDomAdapter.logError @ platform-browser.umd.js:1900BrowserDomAdapter.logGroup @ platform-browser.umd.js:1910ExceptionHandler.call @ core.umd.js:4329(anonymous function) @ core.umd.js:9813schedulerFn @ core.umd.js:6699SafeSubscriber.__tryOrUnsub @ Subscriber.ts:240SafeSubscriber.next @ Subscriber.ts:192Subscriber._next @ Subscriber.ts:133Subscriber.next @ Subscriber.ts:93Subject._finalNext @ Subject.ts:154Subject._next @ Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @ core.umd.js:6687NgZone._zoneImpl.NgZoneImpl.onError @ core.umd.js:9249NgZoneImpl.inner.inner.fork.onHandleError @ core.umd.js:9121ZoneDelegate.handleError @ zone.js:327Zone.runGuarded @ zone.js:233_loop_1 @ zone.js:487drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426
platform-browser.umd.js:1900 STACKTRACE:BrowserDomAdapter.logError @ platform-browser.umd.js:1900ExceptionHandler.call @ core.umd.js:4331(anonymous function) @ core.umd.js:9813schedulerFn @ core.umd.js:6699SafeSubscriber.__tryOrUnsub @ Subscriber.ts:240SafeSubscriber.next @ Subscriber.ts:192Subscriber._next @ Subscriber.ts:133Subscriber.next @ Subscriber.ts:93Subject._finalNext @ Subject.ts:154Subject._next @ Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @ core.umd.js:6687NgZone._zoneImpl.NgZoneImpl.onError @ core.umd.js:9249NgZoneImpl.inner.inner.fork.onHandleError @ core.umd.js:9121ZoneDelegate.handleError @ zone.js:327Zone.runGuarded @ zone.js:233_loop_1 @ zone.js:487drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426
platform-browser.umd.js:1900 Error: Uncaught (in promise): Error: Cannot match any routes: ''
    at resolvePromise (zone.js:538)
    at zone.js:515
    at ZoneDelegate.invoke (zone.js:323)
    at Object.NgZoneImpl.inner.inner.fork.onInvoke (core.umd.js:9100)
    at ZoneDelegate.invoke (zone.js:322)
    at Zone.run (zone.js:216)
    at zone.js:571
    at ZoneDelegate.invokeTask (zone.js:356)
    at Object.NgZoneImpl.inner.inner.fork.onInvokeTask (core.umd.js:9091)
    at ZoneDelegate.invokeTask (zone.js:355)BrowserDomAdapter.logError @ platform-browser.umd.js:1900ExceptionHandler.call @ core.umd.js:4332(anonymous function) @ core.umd.js:9813schedulerFn @ core.umd.js:6699SafeSubscriber.__tryOrUnsub @ Subscriber.ts:240SafeSubscriber.next @ Subscriber.ts:192Subscriber._next @ Subscriber.ts:133Subscriber.next @ Subscriber.ts:93Subject._finalNext @ Subject.ts:154Subject._next @ Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @ core.umd.js:6687NgZone._zoneImpl.NgZoneImpl.onError @ core.umd.js:9249NgZoneImpl.inner.inner.fork.onHandleError @ core.umd.js:9121ZoneDelegate.handleError @ zone.js:327Zone.runGuarded @ zone.js:233_loop_1 @ zone.js:487drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426
zone.js:461 Unhandled Promise rejection: Cannot match any routes: '' ; Zone: angular ; Task: Promise.then ; Value: Error: Cannot match any routes: ''(…)consoleError @ zone.js:461_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426
zone.js:463 Error: Uncaught (in promise): Error: Cannot match any routes: ''(…)consoleError @ zone.js:463_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426

main.ts file is

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS } from './app.routes';


bootstrap(AppComponent, [
  APP_ROUTER_PROVIDERS
]);

and my app.routes.ts is

import { provideRouter, RouterConfig }  from '@angular/router';
import { AboutComponent } from './about/about.component';

  export const routes: RouterConfig = [
{
  path : 'about',
  component : AboutComponent
}
];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes)
];

my app.component.ts file is

import { Component }       from '@angular/core';
import { AboutComponent } from './about/about.component';
import { ROUTER_DIRECTIVES }  from '@angular/router';



@Component({
  selector: 'my-app',
  template: `
    <h1> this is our app.component.ts</h1>
    <h2>{{title}}</h2>

    <a [routerLink]="['/about']">ABOUT ADSELINT</a>
    <router-outlet></router-outlet>

      `,
  directives: [ROUTER_DIRECTIVES]

})
export class AppComponent {
  title = 'app.component';
}

my about.component.ts is

import { Component } from '@angular/core';



@Component({
  selector: 'my-about',
  templateUrl : 'app/about/about.html'

})
export class AboutComponent {
  abt = 'ABOUT ';
}

and about.html contains

<h1>hello</h1>

it shows the proper output but while loading only the localhost i.e app.component.ts it ws showing the above error.

like image 406
virushree Avatar asked Mar 11 '23 18:03

virushree


1 Answers

Issue is this

When you load you app URL is localhost:3000 and hence you need to provide a path which is by default path which is path:'' and hence you are not providing this path this Error is coming .

Solution Add the below path to app.routes.ts file

app.routes.ts

import { provideRouter, RouterConfig }  from '@angular/router';
import { AboutComponent } from './about/about.component';
import {AppComponent} from './app.component';

export const routes: RouterConfig = [
  {
    path: '',
    redirectTo: '/about',
    pathMatch: 'full'
  },
{
  path : 'about',
  component : AboutComponent
}
];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes)
];

And your problem will be solved

like image 106
Sandeep Chikhale Avatar answered Mar 19 '23 05:03

Sandeep Chikhale