2

HI I have strange issue and don't understand how to fix that. So I have service that check if you logged in when go to some url in web app. If not it redirect to home page.

My service`

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';

@Injectable()
export class AuthService  implements CanActivate {
    private userLogedin: string = localStorage.getItem('AUTHENTICATION');

    constructor(private router: Router) {}

    canActivate() {
        if(this.userLogedin === 'true') {
            return true;
        }

        this.router.navigateByUrl('/');
        return false;
    }
}

My module routing

import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';

import { LandingPageComponent } from 'app/components/LandingPageComponent/LandingPageComponent';

const landingPageRoutes: Routes = [
    {path: '', component: LandingPageComponent, pathMatch: 'full'}

    ];

export const landingPageRouting: ModuleWithProviders = RouterModule.forChild(landingPageRoutes);

Router canactivate module.routing

import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';

import { ProfilePageComponent } from 'app/components/ProfilePageComponent/ProfilePageComponent';
import { AuthService } from 'app/shared/AuthService';


const profilePageRoutes: Routes = [
    {path: 'profile', component: ProfilePageComponent, canActivate: [AuthService]  }
    ];

export const profilePageRouting: ModuleWithProviders = RouterModule.forChild(profilePageRoutes);

But when hi redirect to home page it gives me a blank page.

Aram Mkrtchyan
  • 2,690
  • 4
  • 31
  • 47

1 Answers1

0

So If you have this problem try this RouterModule.forRoot(appRoutes, { useHash: true })

Aram Mkrtchyan
  • 2,690
  • 4
  • 31
  • 47