Base solution for your next web application
Open Closed

Problem redirecting to anything else but main/dashboard #3692


User avatar
0
bilalhaidar created

Hello, I am trying to change the default redirect in an Angular 2 app. It is not working. I changed the app-routing.module.ts to something like this:

'/poc' is a route defined inside the 'sms' module as shown below.

Why would it not work?

Thanks

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NotificationsComponent } from './shared/layout/notifications/notifications.component';
import { AppComponent } from './app.component';
import { AppRouteGuard } from './shared/common/auth/auth-route-guard';

@NgModule({
    imports: [
        RouterModule.forChild([
            {
                path: 'app',
                component: AppComponent,
                canActivate: [AppRouteGuard],
                canActivateChild: [AppRouteGuard],
                children: [
                    {
                        path: '',
                        children: [
                            { path: 'notifications', component: NotificationsComponent },
                            { path: '', redirectTo: '/poc', pathMatch: 'full' }
                        ]
                    },
                    {
                        path: 'main',
                        loadChildren: 'app/main/main.module#MainModule', //Lazy load main module
                        data: { preload: true }
                    },
                    {
                        path: 'admin',
                        loadChildren: 'app/admin/admin.module#AdminModule', //Lazy load admin module
                        data: { preload: true }
                    },
                    {
                        path: 'sms',
                        loadChildren: 'app/sms/sms.module#SmsModule', //Lazy load sms module
                        data: { preload: true }
                    }
                ]
            }
        ])
    ],
    exports: [RouterModule]
})
export class AppRoutingModule { }

6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry for the late reply. You can modify this function to change default route <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/angular/src/app/shared/common/auth/auth-route-guard.ts#L43">https://github.com/aspnetzero/aspnet-ze ... ard.ts#L43</a>.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Thanks Ismail.

    Im fact, I am not yet getting why the need for this function in a time canActivate is doing the job? Can you illustrate more please?

    Isn't better to keep the default Angular 2 routing?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bilalhaidar,

    I don't know how I can tell you more than this :). Your main request was changing the default redirect route and you can do it by changing auth-route-guard.ts file.

  • User Avatar
    0
    bilalhaidar created

    Hahaha thanks Ismail. I meant from my question why not count only on the canActivate why need that function to redirect. That's my point. Isnt it duplicating the checking?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bilalhaidar,

    I think this was the case but I don't remember very well. When a user navigates to a route that user does not have permission, we wanted to redirect user to a default page that he/she has access to. I think this is not possible with default behaviour.

  • User Avatar
    0
    bilalhaidar created

    Good to know. Thanks a lot