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

相关内容

热门资讯

透视辅助!wpk俱乐部怎么作弊... 透视辅助!wpk俱乐部怎么作弊,wpk俱乐部有没有辅助-真是一直总是有辅助神器(哔哩哔哩)1)有没有...
透视揭幕!德普之星透视辅助,德... 透视揭幕!德普之星透视辅助,德普之星透视辅助插件-一贯真的是有辅助攻略(哔哩哔哩)该软件可以轻松地帮...
透视揭露!红龙poker辅助平... 透视揭露!红龙poker辅助平台,epoker透视-其实有辅助教程(哔哩哔哩)1、起透看视 红龙po...
透视辅助!wepoker黑侠辅... 透视辅助!wepoker黑侠辅助器,wepoker插件功能辅助器-一贯有辅助软件(哔哩哔哩)破解侠是...
透视辅助!wepoker辅助软... 透视辅助!wepoker辅助软件价格,wejoker开挂-竟然有辅助工具(哔哩哔哩)1、起透看视 辅...
透视详情!wpk模拟器是什么,... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
透视专业!德普之星的辅助工具介... 透视专业!德普之星的辅助工具介绍,德普软件-总是真的是有辅助软件(哔哩哔哩)1、玩家可以在透视最简单...
透视解谜!wepoker数据分... 透视解谜!wepoker数据分析工具,wepoker免费脚本-确实是有辅助工具(哔哩哔哩)1、进入游...
透视解谜!德普之星有透视辅助吗... 透视解谜!德普之星有透视辅助吗,德普之星的辅助工具介绍-一直有辅助神器(哔哩哔哩)1、辅助软件下载优...
透视分享!wpk透视是真的假的... 透视分享!wpk透视是真的假的,wpk德州局透视-真是真的有辅助app(哔哩哔哩)1、德州局透视免费...