1

I am getting following two errors.

1) router-outlet is not a known element for following code

import {Routes, RouterModule}from "@angular/router"


@Component({
  selector: 'app-root',
  template: `
    <top-menu></top-menu>
    <router-outlet></router-outlet>
  `
})

2) Unexpected closing tag <a> for following code

import {Routes, RouterModule} from "@angular/router"

@Component({
  selector: 'top-menu',
  template: `    
    <a [routerLink]=['new']> New </a>
      <a [routerLink]=['list']> List </a>
  `
})

The routes are defined as follows:

export const routes:Routes = [
  {path:'new', component:MyParaComponent},
  {path:'list', component:MyListComponent}
];

export const appRouterModule =RouterModule.forRoot(routes);

Interestingly, if I add appRouterModule in imports in app.module.ts then I get an error that app.routes.ts (the file where appRouterModule) is defined doesn't export any module.

.../app/app.routes"' has no exported member 'appRouterModule'.

8
  • What does your @NgModule's look like? Commented Jul 12, 2017 at 13:14
  • Following is the link to cut-down version in Plunker - plnkr.co/edit/hLME9NSoXeSuLp7MxtGw?p=preview Commented Jul 12, 2017 at 13:41
  • For simplicity "<a routerLink='new'>New</a>" can be used, if you are not passing any fragment, or dynamic id etc. Commented Jul 12, 2017 at 13:42
  • I originally tried without double quotes but it didnt work either Commented Jul 12, 2017 at 13:47
  • 1
    @Malik is correct. Manu - Have a look at the official routing guide angular.io/guide/router - there is a useful live example on the page, in addition to plenty of info how to use Angular Routing Commented Jul 12, 2017 at 14:02

1 Answer 1

1

Corrected

Part 1: You need to import RouterOutlet from "@angular/router", and instead of importing RouterModule into your component, you should import it into your the parent Module (if you arent already).

Part 2:

<a [routerLink]=['new']> New </a> should be <a [routerLink]="['new']"> New </a>

Sign up to request clarification or add additional context in comments.

1 Comment

Following is the link to cut-down version in Plunker - plnkr.co/edit/hLME9NSoXeSuLp7MxtGw?p=preview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.