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?
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With