Reputation: 9826
I'm new to Angular
and writing my first Angular
application for admin dashboard.
I have to setup two layouts
for that I have setup two layout components
and put all auth components in auth module
The directory structure of my application is like
app
|- e2e
|- node_modules
|- src
|- app
|- auth (module)
|- login (component)
|- login.component.ts
|- login.component.html
|- register (component)
|- register.component.ts
|- register.component.html
|- auth.module.ts
|- dashboard (component for auth users)
|- dashboard.component.ts
|- dashboard.component.html
|- layouts ( directory)
|- admin-layout (component)
|- admin-layout.component.html
|- admin-layout.component.ts
|- admin-layout.module.ts
|- admin-layout.routing.ts
|- auth-layout (component)
|- auth-layout.component.html
|- auth-layout.component.ts
|- auth-layout.module.ts
|- auth-layout.routing.ts
|- app.component.html
|- app.component.ts
|- app.module.ts
|- app-routing.module.ts
|- assets
|- index.html
content of app-routing.module.ts
import { NgModule } from '@angular/core';
import {AppRoutingModule} from './app-routing.module';
import { AppComponent } from './app.component';
import {FormsModule} from '@angular/forms';
import {RouterModule} from '@angular/router';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {HttpClientModule} from '@angular/common/http';
import { AdminLayoutComponent } from './layouts/admin-layout/admin-layout.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {ComponentsModule} from './components/components.module';
import { AuthLayoutComponent } from './layouts/auth-layout/auth-layout.component';
@NgModule({
declarations: [
AppComponent,
AdminLayoutComponent,
AuthLayoutComponent
],
imports: [
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
RouterModule,
AppRoutingModule,
NgbModule.forRoot(),
ComponentsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
content of app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {AdminLayoutComponent} from './layouts/admin-layout/admin-layout.component';
import {CommonModule} from '@angular/common';
import {BrowserModule} from '@angular/platform-browser';
import {AuthLayoutComponent} from './layouts/auth-layout/auth-layout.component';
const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: '', component: AdminLayoutComponent, children: [
{path: '', loadChildren: './layouts/admin-layout/admin-layout.module#AdminLayoutModule'}
] },
{ path: '', component: AuthLayoutComponent, children: [
{path: '', loadChildren: './layouts/auth-layout/auth-layout.module#AuthLayoutModule'}
]}
];
@NgModule({
imports: [
CommonModule,
BrowserModule,
RouterModule.forRoot(routes)
],
exports: [
RouterModule
]
})
export class AppRoutingModule { }
content of admin-layout.routing.ts
import { Routes } from '@angular/router';
import {DashboardComponent} from '../../dashboard/dashboard.component';
export const AdminLayoutRoutes: Routes = [
{ path: 'dashboard', component: DashboardComponent}
];
content of admin-layout.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AdminLayoutRoutes } from './admin-layout.routing';
import { DashboardComponent } from '../../dashboard/dashboard.component';
import { IconsComponent } from '../../icons/icons.component';
import {ChartsModule} from 'ng2-charts';
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(AdminLayoutRoutes),
FormsModule,
ChartsModule,
NgbModule
],
declarations: [
DashboardComponent,
IconsComponent
]
})
export class AdminLayoutModule { }
content of auth-layout.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {RouterModule} from '@angular/router';
import {AuthLayoutRoutes} from './auth-layout.routing';
import {FormsModule} from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {AuthModule} from '../../auth/auth.module';
import {LoginComponent} from '../../auth/login/login.component';
import {RegisterComponent} from '../../auth/register/register.component';
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(AuthLayoutRoutes),
FormsModule,
NgbModule,
AuthModule
],
declarations: [
LoginComponent,
RegisterComponent
]
})
export class AuthLayoutModule { }
content of auth-layout.routing.ts
import {Routes} from '@angular/router';
import {LoginComponent} from '../../auth/login/login.component';
import {RegisterComponent} from '../../auth/register/register.component';
import {ForgotPasswordComponent} from '../../auth/forgot-password/forgot-password.component';
import {ResetPasswordComponent} from '../../auth/reset-password/reset-password.component';
export const AuthLayoutRoutes: Routes = [
{path: 'login', component: LoginComponent},
{path: 'register', component: RegisterComponent},
{path: 'forgot-password', component: ForgotPasswordComponent},
{path: 'reset-password', component: ResetPasswordComponent}
];
Question
On accessing localhost:4200
is redirecting to localhost:4200/dashboard
and it's working as dashboard
path is set in admin routing.
But on accessing localhost:4200/login
is redirecting to localhost:4200
and login routing is not working from auth routing.
App code url: https://stackblitz.com/edit/angular-lwevqj
App url: https://angular-lwevqj.stackblitz.io
Upvotes: 3
Views: 25819
Reputation: 103
It is redirecting to /dashboard
as your global routing file has defined on the top to redirect any calls to Dashboard component.
See the first path
definition in your global routing file
In this angular project the hierarchy is built up. It has been assumed that the user will go to dashboard first.
If you want to go directly to Login Component just add /login
for the login component route in your global routing file.
But please take care of routing in your login module. I would suggest to use child routing of the form /child1/child2
only if you really want to have that dependent url.
If you want to make some of your components to be of the form /name
, they should be present globally.
You can see more on Angular 6 Docs
Upvotes: 0
Reputation: 2224
you are using this code { path: '', redirectTo: 'dashboard', pathMatch: 'full' } in (app-routing.module.ts) file so on accessing localhost:4200 is redirecting to localhost:4200/dashboard and it's working as dashboard.
firstly set your by default path location where you want to redirect like { path: '', redirectTo: 'login', pathMatch: 'full' } if you will use this code then your url automatic redirect to login component when you will enter localhost:4200.
Upvotes: 0
Reputation: 1868
Your routes should be like -
const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'admin', component: AdminLayoutComponent, children: [
{path: '', loadChildren: './layouts/admin-layout/admin-layout.module#AdminLayoutModule'}
] },
{ path: 'auth', component: AuthLayoutComponent, children: [
{path: '', loadChildren: './layouts/auth-layout/auth-layout.module#AuthLayoutModule'}
]}
];
Also your auth routes should be like -
export const AuthLayoutRoutes: Routes = [
{path: '', component: LoginComponent},
{path: 'register', component: RegisterComponent},
{path: 'forgot-password', component: ForgotPasswordComponent},
{path: 'reset-password', component: ResetPasswordComponent}
];
Calls should be -
Login - localhost:4200/auth/login
Register - localhost:4200/auth/register
Similarly others
Upvotes: 8
Reputation: 1191
You should change your main routes (replace '' with 'login'), I think the problem is here:
const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: '', component: AdminLayoutComponent, children: [
{path: '', loadChildren: './layouts/admin-layout/admin-layout.module#AdminLayoutModule'}
] },
{ path: 'login', component: AuthLayoutComponent, children: [
{path: '', loadChildren: './layouts/auth-layout/auth-layout.module#AuthLayoutModule'}
]}
];
Upvotes: 2