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、在wepoke...
据权威媒体报道!wepoker... 据权威媒体报道!wepoker底牌透视脚本(透视)真是真的是有辅助修改器(发现有挂)-哔哩哔哩1、每...
现有说明如下!约局吧能不能开挂... 现有说明如下!约局吧能不能开挂(透视)都是存在有辅助工具(有挂透视)-哔哩哔哩1、下载好约局吧能不能...
现有说明如下!wepoker怎... 现有说明如下!wepoker怎么挂飞机(透视)切实是真的辅助平台(有人有挂)-哔哩哔哩1、下载好we...
备受关注的!wejoker辅助... 备受关注的!wejoker辅助机器人(透视)果然存在有辅助平台(真实有挂)-哔哩哔哩1、wejoke...
据文件显示!hhpoker可以... 您好,hhpoker可以控制牌吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
做出回应!wepoker俱乐部... 做出回应!wepoker俱乐部辅助(透视)本来存在有辅助工具(有挂教学)-哔哩哔哩1、用户打开应用后...
连日来!hhpoker透视脚本... 连日来!hhpoker透视脚本视频(透视)本来存在有辅助修改器(有挂攻略)-哔哩哔哩连日来!hhpo...
在玩家背景下!wpk俱乐部有没... 在玩家背景下!wpk俱乐部有没有辅助(透视)都是真的是有辅助辅助器(有挂方法)-哔哩哔哩wpk俱乐部...
不少玩家反映!hhpoker透... 不少玩家反映!hhpoker透视脚本视频(透视)好像是真的辅助插件(有挂技术)-哔哩哔哩1、下载好h...