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

相关内容

热门资讯

揭秘几款!poker辅助器免费... 揭秘几款!poker辅助器免费安装(辅助挂)其实是有挂(有挂方针辅助器)1、全新机制【ai辅助工具激...
分享认知!pokemomo辅助... 分享认知!pokemomo辅助工具(辅助挂)原来是真的有挂(有挂工具辅助软件)1、很好的工具软件,可...
终于知道!hhpoker脚本(... 终于知道!hhpoker脚本(辅助挂)好像有挂(有挂秘诀辅助脚本)1、打开软件启动之后找到中间准星的...
玩家攻略!哈糖大菠萝挂(辅助挂... 玩家攻略!哈糖大菠萝挂(辅助挂)都是是真的有挂(有挂教程辅助工具)1、用户打开应用后不用登录就可以直...
揭秘攻略!wepoker黑侠辅... 揭秘攻略!wepoker黑侠辅助器(辅助挂)竟然有挂(有挂透视辅助插件)1、许多玩家不知道辅助怎么退...
最新技巧!wepoker透视器... 最新技巧!wepoker透视器免费(辅助挂)原来真的是有挂(有挂头条辅助插件)运辅助工具,进入游戏界...
重大通报!hhpoker怎么破... 重大通报!hhpoker怎么破解(辅助挂)总是有挂(今日头条辅助器)重大通报!hhpoker怎么破解...
玩家必看科普!云扑克有透视吗(... 玩家必看科普!云扑克有透视吗(辅助挂)好像真的是有挂(果真有挂辅助方法)1、用户打开应用后不用登录就...
我来教大家!htx矩阵wepo... 我来教大家!htx矩阵wepoker辅助(辅助挂)其实存在有挂(证实有挂辅助技巧)1. 选牌创建新账...
程序员教你!pokerworl... 程序员教你!pokerworld辅助器(辅助挂)真是存在有挂(有挂攻略辅助教程)1、打开软件启动之后...