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

相关内容

热门资讯

重大通报!wepoker脚本下... 重大通报!wepoker脚本下载(辅助挂)总是存在有挂(有挂助手辅助攻略)1、完成辅助器v3.3的残...
必备教程!德普之星app安卓版... 必备教程!德普之星app安卓版破解版(辅助挂)果然是有挂(有挂规律辅助方法)进入游戏-大厅左侧-新手...
玩家亲测!pokerworld... 玩家亲测!pokerworld修改器(辅助挂)本来是真的有挂(有挂详情辅助器)1、脚本辅助下载、免费...
程序员教你!约局吧德州真的存在... 程序员教你!约局吧德州真的存在透视吗(辅助挂)都是是有挂(有挂技术辅助软件)1、首先打开辅助器下载最...
盘点十款!aapoker透视脚... 盘点十款!aapoker透视脚本下载(辅助挂)一贯有挂(存在有挂辅助方法)1、实时透视辅助更新:用户...
安装程序教程!wepoker公... 安装程序教程!wepoker公共底牌(辅助挂)总是真的有挂(有挂详情辅助软件)1、有没有辅助教程、有...
免费测试版!wepoker好友... 免费测试版!wepoker好友局透视(辅助挂)真是是有挂(有挂规律辅助方法)1、玩家可以在线上大神俱...
实操分享!wepoker亲友圈... 实操分享!wepoker亲友圈有用吗(辅助挂)总是真的是有挂(有挂分享辅助工具)1、玩家可以在线上大...
研究成果!hhpoker真的有... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
推荐一款!wepoker插件下... 推荐一款!wepoker插件下载(辅助挂)竟然有挂(真是有挂辅助工具)1、这是跨平台的轻量版有透视,...