Angular: What's the purpose of paths in app-routing.module.ts

I'm new in Angular and asked myself what the purpose of the paths in app-routing.module.ts (and so the general purpose of this module) is when all my paths are handled in app.module.ts?

This is my app-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TaskviewComponent } from './taskview/taskview.component';

const routes: Routes = [];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

This is my app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import {RouterModule} from '@angular/router';
import { TaskviewComponent } from './taskview/taskview.component';
import { AppRoutingModule } from './app-routing.module';
import { AddTaskComponent } from './add-task/add-task.component';
import {ReactiveFormsModule} from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    TaskviewComponent,
    AddTaskComponent
  ],
  imports: [
    RouterModule.forRoot([{
      path: 'view', component: TaskviewComponent},
      {path: 'add', component: AddTaskComponent
    }]),
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Upvotes: 3

Views: 4882

Answers (2)

famoha
famoha

Reputation: 435

You are using app-routing.module.ts wrong (you are not using it at all).

The purpose of app-routing.module.ts is to outsource your routing in a different file (purely for readability purposes) and then import the module back inside your app.module.ts file.

The correct way of using it would be:

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TaskviewComponent } from './taskview/taskview.component';

const routes: Routes = [
      {path: 'view', component: TaskviewComponent},
      {path: 'add', component: AddTaskComponent}
      ];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { TaskviewComponent } from './taskview/taskview.component';
import { AppRoutingModule } from './app-routing.module';
import { AddTaskComponent } from './add-task/add-task.component';
import {ReactiveFormsModule} from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    TaskviewComponent,
    AddTaskComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Upvotes: 8

NeNaD
NeNaD

Reputation: 20334

You can put code from app-routing.module.ts in app.module.ts. It is just the good practice to have routes defined in separate file.

Upvotes: 2

Related Questions