Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular : Couldn't find a route declaration in /src/app/app-routing.module.ts

When I try to add automatic routing for components generated using following command

ng generate module orders --route orders --module app.module

I get the error

Couldn't find a route declaration in /src/app/app-routing.module.ts.

app-routing.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes } from '@angular/router';


@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class AppRoutingModule {

 }

I also tried adding this line the app-routing file

export const routes: Routes = []

Still problem persits.How to fix this error?

like image 604
Arjun Avatar asked Mar 27 '20 12:03

Arjun


People also ask

What is router link in Angular?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes. Let's explore how to use RouterLink , Router.

What is router outlet Angular 8?

The router-outlet is a directive that's available from the @angular/router package and is used by the router to mark where in a template, a matched component should be inserted. Thanks to the router outlet, your app will have multiple views/pages and the app template acts like a shell of your application.


2 Answers

I faced the same problem when was using angular template. Solution was to write routing file name explicitly

ng generate module customers --route customers --module app.routing.ts
like image 162
glx Avatar answered Sep 24 '22 18:09

glx


Try to use command ng generate module orders --route orders --module app-routing.module, as described in cli module documentation :

--route The route path for a lazy-loaded module. When supplied, creates a component in the new module, and adds the route to that component in the Routes array declared in the module provided in the --module option.

Looks like tutorial cli command (ng generate module customers --route customers --module app.module) works fine only while your file structure is close to default and it can easily find routes array.

like image 41
Petrovich Deferenty Avatar answered Sep 25 '22 18:09

Petrovich Deferenty