微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular 2 – 页面加载时的许多历史条目

当我在新的浏览器窗口中打开我的Angular 2应用程序时,我在浏览器页面历史记录中获得了10个条目.这发生在路由器中列出的任何页面上,没有指定页面(即 http://localhost:8080http://localhost:8080/survey等)

const routes: Routes = [
    {path: 'survey',component: SurveyComponent,canActivate: [AuthGuard]},{path: 'survey/:id',{path: '',component: HomeComponent},{path: 'about',component: AboutComponent},{path: 'terms',component: TermsAndConditionsComponent},{path: 'map',component: MapComponent},{path: 'what-next',component: WhatNextComponent},{path: 'contact',component: ContactComponent},{path: '**',redirectTo: '',pathMatch: 'full'},];

我正在使用路由器3.1.2

"@angular/router": "3.1.2",

enter image description here

我从去年3月开始发现similar question,但答案声称已经在Angular的代码解决了.

更新
根据要求,这是AuthGuard代码

import {Injectable} from '@angular/core';
import {AuthenticationService} from '../_services/index';
import {LoginModalService} from "../login_modal/login_modal.service";
import { Router,CanActivate,ActivatedRouteSnapshot,RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class AuthGuard implements CanActivate {

    constructor(private authentication: AuthenticationService,private loginModalService: LoginModalService) {
    }

    canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot) {
        if (this.authentication.isAuthenticated()) {
            return Observable.of(true);
        } else {
            this.loginModalService.toggleVisable(state.url);
            return Observable.of(false);
        }
    }
}

解决方法

我有类似的情况,我通过延迟加载路线解决
也就是说,您可以将路径分离到主要路径加载中的不同模块路由广告,例如thi

在主要路线

{
        path: 'dashboard',component: DashboardComponent,},{
        path: 'user',loadChildren: 'app/user/user.module#UserModule',//in user module
      },

所以在你的用户模块中

@NgModule ( {
  imports: [
   userRoutingModule //contains my user module routes
  ],} )
export class UserModule {

 }

用户路由模块有

const userRoutes: Routes = [
        { path: '',component: UserComponent},];

 @NgModule({
  imports: [
    RouterModule.forChild(userRoutes)
   ],exports: [RouterModule],providers: []
   })
 export class userRoutingModule { }

这是我如何解决我的问题,我希望这有帮助.

注意:记住import.forRoot的主要路线

在这种情况下,请确保在根模块中(您可以在app模块中导入用户模块)

@NgModule({
    declarations: [
    ],imports: [

    UserModule,//in your case maybe use survey module

    ],})

  export class AppModule {}  //this is in my root module

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐