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 无限加载的问题。

相关内容

热门资讯

透视挂透视“斗棋联盟辅助”最初... 透视挂透视“斗棋联盟辅助”最初有辅助平台(有挂猫腻);无需打开直接搜索打开薇:136704302 咨...
一分钟揭秘“奇迹陕西三代辅助器... 一分钟揭秘“奇迹陕西三代辅助器免费”从来有辅助挂(有挂细节);无需打开直接搜索加薇136704302...
发现一款“拱趴大菠萝挂怎么安装... 【亲,拱趴大菠萝挂怎么安装 这款游戏可以开挂的,确实是有挂的,很多玩家在这款拱趴大菠萝挂怎么安装中打...
玩家必看攻略“威信茶馆跑辅助器... 玩家必看攻略“威信茶馆跑辅助器”原先有辅助安装(有挂规律)您好:威信茶馆跑辅助器这款游戏可以开挂,确...
总算明白“雀神挂件怎么安装”素... 总算明白“雀神挂件怎么安装”素来有开挂辅助安装(有挂方式)这是一款可以让一直输的玩家,快速成为一个“...
透视好友房“微乐小程序游戏破解... 您好:微乐小程序游戏破解器下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
透视安卓版“贰柒拾智能辅助柒”... 透视安卓版“贰柒拾智能辅助柒”本来有开挂辅助工具(有挂技巧);无需打开直接搜索微信(13670430...
总算了解“中至赣牌圈挂件价格明... 大家好,今天小编来为大家解答中至赣牌圈挂件价格明细这个问题咨询软件客服可以免费测试直接加微信(136...
透视游戏“广东闲来辅助神器免费... 透视游戏“广东闲来辅助神器免费版”先前有辅助开挂挂(有挂技巧);无需打开直接搜索薇:13670430...
一分钟教会你“上饶中至辅助插件... 一分钟教会你“上饶中至辅助插件透视”原先有辅助开挂工具(有挂存在);无需打开直接搜索加薇136704...