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

相关内容

热门资讯

推荐透视!xpoker怎么作必... 推荐透视!xpoker怎么作必弊!其实是真的有辅助攻略(有挂方法)-哔哩哔哩xpoker怎么作必弊能...
曝光透视!sohoo poke... 曝光透视!sohoo poker辅助器!都是真的有辅助脚本(有挂功能)-哔哩哔哩1、玩家可以在soh...
普及透视!pokermaste... 普及透视!pokermaster破解版!确实真的有辅助方法(新版有挂)-哔哩哔哩1、pokermas...
详细透视!pokemmo脚本辅... 详细透视!pokemmo脚本辅助器下载!都是一直总是有辅助app(真是有挂)-哔哩哔哩;pokemm...
解谜透视!pokemmo手机辅... 解谜透视!pokemmo手机辅助软件!切实一直总是有辅助技巧(揭秘有挂)-哔哩哔哩1、进入到poke...
详细透视!德州局透视脚本下载安... 详细透视!德州局透视脚本下载安装最新版本!好像有辅助脚本(有挂教程)-哔哩哔哩1、德州局透视脚本下载...
总结透视!xpoker辅助工具... 您好,xpoker辅助工具这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
揭露透视!pokemmo手机版... 揭露透视!pokemmo手机版透视脚本!其实存在有辅助软件(的确有挂)-哔哩哔哩1、pokemmo手...
开挂透视!佛手在线大菠萝辅助!... 开挂透视!佛手在线大菠萝辅助!果然一直都是有辅助脚本(讲解有挂)-哔哩哔哩1、佛手在线大菠萝辅助透视...
专业透视!约局吧怎么看有没有挂... 专业透视!约局吧怎么看有没有挂!总是存在有辅助方法(证实有挂)-哔哩哔哩1、完成约局吧怎么看有没有挂...