AngularUniversal无限加载错误
创始人
2024-10-29 21:01:26
0
  1. 在 Angular Universal 的服务器渲染配置中添加一个选项 "initialNavigation" 并将其值设置为 false。如下所示:

    export const appRoutes: Routes = [ { path: '', component: HomeComponent, pathMatch: 'full' }, { path: 'about', component: AboutComponent }, { path: '**', component: NotFoundComponent } ];

    export const appRoutingProviders: any[] = [];

    export const appRouting: ModuleWithProviders = RouterModule.forRoot(appRoutes, { initialNavigation: false });

  2. 在你的组件中添加一些代码以检测服务器和浏览器。如下所示:

    import { isPlatformBrowser } from '@angular/common'; import { Component, Inject, PLATFORM_ID } from '@angular/core';

    @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app';

    constructor(@Inject(PLATFORM_ID) private platformId: Object) { if (isPlatformBrowser(this.platformId)) { // 这里是浏览器端的代码 } else { // 这里是服务器端的代码 } } }

  3. 在你的组件中添加一些代码以检测页面是否已经初始化完成。如下所示:

    import { Component, OnDestroy, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Subscription } from 'rxjs';

    @Component({ selector: 'app-about', templateUrl: './about.component.html', styleUrls: ['./about.component.css'] }) export class AboutComponent implements OnInit, OnDestroy { private routeSub$: Subscription;

    constructor(private route: ActivatedRoute) {}

    ngOnInit() { this.routeSub$ = this.route.url.subscribe(() => { // 这里是页面初始化完成后执行的代码 }); }

    ngOnDestroy() { this.routeSub$.unsubscribe(); } }

通过以上代码示例的改动,可以有效地解决 Angular Universal 无限加载的问题。

相关内容

热门资讯

2024教程!智星菠萝德州辅助... 2024教程!智星菠萝德州辅助挂多功能透视工具,切实是有挂(了解有挂)1、每一步都需要思考,不同水平...
分享给玩家!AAPoKer辅助... 分享给玩家!AAPoKer辅助挂多功能透视工具,一直真的是有挂(详细教程)1、打开软件启动之后找到中...
揭秘攻略!智星菠萝德州辅助挂多... 揭秘攻略!智星菠萝德州辅助挂多功能透视工具,总是是真的有挂(有挂方针)1、每一步都需要思考,不同水平...
一起来探讨!红龙poker辅助... 一起来探讨!红龙poker辅助挂多功能透视工具,一直是有挂(有挂方法)1.红龙poker 选牌创建新...
终于清楚!wepoker辅助挂... 终于清楚!wepoker辅助挂多功能透视工具,竟然有挂(有挂分享)wepoker能透视中分为三种模型...
一分钟揭秘!微扑克辅助挂多功能... 一分钟揭秘!微扑克辅助挂多功能透视工具,竟然是真的有挂(有人有挂)1、打开软件启动之后找到中间准星的...
科技揭秘!wpk辅助挂多功能透... 科技揭秘!wpk辅助挂多功能透视工具,确实是真的有挂(有挂解惑)1、实时wpk透视辅助更新:用户可以...
让我来分享经验!红龙poker... 让我来分享经验!红龙poker辅助挂多功能透视工具,好像存在有挂(有挂规律)亲,关键说明,红龙pok...
关于!wepoker辅助挂多功... 关于!wepoker辅助挂多功能透视工具,好像有挂(存在有挂)1、每一步都需要思考,不同水平的挑战w...
重大通报!wepoker辅助挂... 重大通报!wepoker辅助挂多功能透视工具,真是真的是有挂(果真有挂)在进入wepoker软件靠谱...