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

相关内容

热门资讯

透视ai!德普之星透视辅助软件... 透视ai!德普之星透视辅助软件下载,wepoker怎么提高运气,技巧教程(发现有挂)-哔哩哔哩1、完...
透视新版!hhpoker到底可... 透视新版!hhpoker到底可以作弊码,hhpoker辅助挂下载,AI教程(今日头条)-哔哩哔哩1、...
透视游戏!wpk是真的还是假的... 透视游戏!wpk是真的还是假的,德普之星辅助工具如何打开,详细教程(有挂工具)-哔哩哔哩1、德普之星...
透视透视!aapoker怎么提... 透视透视!aapoker怎么提高中牌率,wepoker辅助透视,玩家教程(有挂细节)-哔哩哔哩1)a...
透视了解!大菠萝手游辅助,hh... 透视了解!大菠萝手游辅助,hhpoker是真的还是假的,科技教程(真的有挂)-哔哩哔哩1、让任何用户...
透视了解!竞技联盟辅助,wep... 透视了解!竞技联盟辅助,wepoker祈福有用吗,教你攻略(有挂攻略)-哔哩哔哩1、下载好wepok...
黑科技辅助!wepoke辅助有... 黑科技辅助!wepoke辅助有挂,(Wepoke ios)原来真的有挂,必胜教程(有挂透视)-哔哩哔...
黑科技辅助!wpk有透视辅助,... 黑科技辅助!wpk有透视辅助,(WPK系统)原来真的有挂,教你攻略(详情)-哔哩哔哩;相信小伙伴都知...
黑科技辅助!wepoke可以看... 黑科技辅助!wepoke可以看有外挂,(WePoKe讲解)原来真的有挂,必赢方法(有挂透视)-哔哩哔...
黑科技辅助!微扑克辅助软件,(... 您好:微扑克辅助软件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...