2

嗨,我有一个奇怪的问题,不明白如何解决。所以我有服务可以检查你在访问网络应用程序中的某个 url 时是否登录。如果不是,它会重定向到主页。

我的服务`

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;
    }
}

我的模块路由

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);

路由器可以激活模块。路由

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);

但是当你重定向到主页时,它给了我一个空白页。

4

1 回答 1

0

所以如果你有这个问题,试试这个 RouterModule.forRoot(appRoutes, { useHash: true })

于 2017-03-07T19:19:10.387 回答