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

相关内容

热门资讯

透视代打!wepokerplu... 透视代打!wepokerplus到底是挂了吗,竞技联盟透视插件,举措教程(总是真的有挂)-哔哩哔哩1...
透视规律!HH平台挂,aapo... 透视规律!HH平台挂,aapoker透视脚本下载,总结教程(一贯是有挂)-哔哩哔哩运aapoker透...
透视脚本!wepoker私人局... 透视脚本!wepoker私人局辅助挂,wpk透视辅助,操作教程(一直真的是有挂)-哔哩哔哩1、wep...
透视脚本!德普软件,wepok... 透视脚本!德普软件,wepoker有辅助功能吗,经验教程(其实真的有挂)-哔哩哔哩该软件可以轻松地帮...
透视规律!wpk控制牌是真的吗... 透视规律!wpk控制牌是真的吗,wpk有辅助吗,资料教程(切实是真的挂)-哔哩哔哩1)wpk控制牌是...
透视总结!约局吧德州真的有透视... 透视总结!约局吧德州真的有透视挂吗,wepoker私人定制透视,模板教程(本来是有挂)-哔哩哔哩1、...
透视辅助!poker worl... 透视辅助!poker world辅助器,wepoker底牌透视,阶段教程(一贯存在有挂)-哔哩哔哩透...
透视讲解!hhpoker辅助挂... 透视讲解!hhpoker辅助挂是真的吗,智星德州辅助译码插件靠谱吗,练习教程(原来有挂)-哔哩哔哩1...
透视苹果版!佛手在线是不是有挂... 透视苹果版!佛手在线是不是有挂,aapoker免费透视脚本,学习教程(真是有挂)-哔哩哔哩1、很好的...
透视代打!wepoker辅助软... 透视代打!wepoker辅助软件视频,wepoker透视底牌,指南书教程(其实是有挂)-哔哩哔哩1、...