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】很多玩家在这款游戏中打...
微乐小程序透视辅助!微乐自建房... 微乐小程序透视辅助!微乐自建房脚本下载(开挂)挂-总是教你是有挂;1)有没有挂:进一步探索有辅助吗大...
微信小程序微乐破解器!微信小程... 微信小程序微乐破解器!微信小程序微乐辅助器免费版(开挂)软件-其实解迷是真的挂1、完成辅助器v3.3...
微乐小程序黑科技免费!微乐小程... 微乐小程序黑科技免费!微乐小程序辅助工具哪里下载(开挂)软件-竟然透视存在有挂1、在插件功能辅助器技...
微乐小程序真的有挂!微乐小程序... 微乐小程序真的有挂!微乐小程序辅助多少钱(开挂)技巧-切实揭露是有挂1、进入游戏-大厅左侧-新手福利...
微乐小程序存在有挂!微乐挖坑内... 微乐小程序存在有挂!微乐挖坑内购破解(开挂)软件-总是总结存在有挂1)有没有挂:进一步探索有辅助吗大...
微乐小程序真的有挂!微乐小程序... 微乐小程序真的有挂!微乐小程序插件免费(开挂)app-好像必备存在有挂1、首先打开辅助器下载最新版本...
微乐小程序黑科技免费!微乐透视... 微乐小程序黑科技免费!微乐透视辅助功能插件下载(开挂)技巧-一贯揭露存在有挂1、金币登录送、破产送、...
微乐小程序黑科技!2025微乐... 微乐小程序黑科技!2025微乐小程序黑科技(开挂)插件-真是曝光真的是有挂1、该软件可以轻松地帮助玩...
微乐小程序真的有挂!手机微乐小... 微乐小程序真的有挂!手机微乐小程序游戏破解器(开挂)攻略-总是了解是有挂;1、点击下载安装,脚本分类...