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

相关内容

热门资讯

透视中牌率!pokemmo辅助... 透视中牌率!pokemmo辅助器手机版下载(透视)本来是真的有挂(详细辅助AI教程)1、机器人多个强...
实操分享!拱趴大菠萝万能挂,w... 实操分享!拱趴大菠萝万能挂,wepoker插件程序激活码,实用技巧(有挂攻略)1、玩家可以在wepo...
透视智能ai!pokemmo辅... 透视智能ai!pokemmo辅助器脚本下载(透视)确实存在有挂(详细辅助新版2025教程)1、首先打...
一分钟快速了解!hhpoker... 自定义wepokerplus到底是挂了吗系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑...
透视新版!竞技联盟透视(透视)... 透视新版!竞技联盟透视(透视)一贯存在有挂(详细辅助曝光教程);1)辅助挂:进一步探索辅助透视大陆,...
终于知道!hhpoker免费辅... 终于知道!hhpoker免费辅助器,pokemmo手机版修改器,教你攻略(有挂辅助);最新版2024...
透视安卓版!aapoker透视... 透视安卓版!aapoker透视脚本下载(透视)好像是有挂(详细辅助黑科技教程);1、aapoker透...
辅助透视!德扑圈透视(透视)一... 辅助透视!德扑圈透视(透视)一贯真的有挂(详细辅助解说技巧)1、玩家可以在软件透明挂俱乐部对游戏的模...
我来教教大家!hhpoker辅... 我来教教大家!hhpoker辅助软件是真的么,wepoker轻量版透视方法,2025版教程(有挂透视...
透视肯定!拱趴大菠萝开挂方法(... 透视肯定!拱趴大菠萝开挂方法(透视)真是真的是有挂(详细辅助可靠教程);小薇(透视辅助)致您一封信;...