Manigandan
Manigandan

Reputation: 11

I have used two or more module in angular and how can i setting routing module with page not found component

How can i fix my problem.. here always redirect to page not found component when i call user module routing path like http://localhost:4200/login when i remove { path: '**', component: PageNotFoundComponent } this code from app routing it will work perfect.. i want page not found component also.. how can i fix it.. please help me.

The app routing module code is

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

import { Routes, RouterModule } from '@angular/router';
import { UserModule } from './_modules/user/user.module';
import { HomeComponent } from './_components/home/home.component';
import { PageNotFoundComponent } from './_components/page-not-found/page-not-found.component';


const routes: Routes = [
  {path:"",component:HomeComponent},
  { path: 'user',loadChildren: () => UserModule},
  { path: '**', component: PageNotFoundComponent }
];

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

my app module code is

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

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UserModule } from './_modules/user/user.module';
import { HomeComponent } from './_components/home/home.component';
import { PageNotFoundComponent } from './_components/page-not-found/page-not-found.component';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    UserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

my new module is user module code like

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

import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';
import { RolesComponent } from './roles/roles.component';
import { ProfileComponent } from './profile/profile.component';
import { ChangePasswordComponent } from './change-password/change-password.component';
import { EditProfileComponent } from './edit-profile/edit-profile.component';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import {FormsModule} from '@angular/forms';
import { UserRoutingModule } from './user-routing.module';
@NgModule({
  declarations: [LoginComponent, RolesComponent, ProfileComponent, ChangePasswordComponent, EditProfileComponent],
  imports: [
    CommonModule,
    BrowserModule,
    HttpClientModule,
    FormsModule,
    UserRoutingModule
  ],
  exports:[]
})
export class UserModule { }

my user module routing code like

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

import { Routes, RouterModule } from '@angular/router';
import { ChangePasswordComponent } from './change-password/change-password.component';
import { LoginComponent } from './login/login.component';


const routes: Routes = [
  {
    path:"login",
    component:LoginComponent
  },
  {
    path:"change-password",
    component:ChangePasswordComponent
  }
];

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

Upvotes: 1

Views: 43

Answers (1)

dash
dash

Reputation: 387

try:

{ path: '**',  pathMatch:'full', component: 'PageNotFoundComponent'}

or

{path: '404' , component: 'PageNotFoundComponent'}
{path: '**' , redirectTo: '/404'}

Upvotes: 2

Related Questions