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机... 德州AI智能辅助机器人!ai机器人代打德州,德州wepower都是有挂(详细ai机器人教程)1、每一...
wepoke插件!wepoke... wepoke插件!wepoke用模拟器,wepower总是真的有挂,技巧教程(有挂解说)1、玩家可以...
wpk发牌逻辑!wPK好像真的... wpk发牌逻辑!wPK好像真的有挂,WPk一般真的有挂,AI教程(有挂解密)1、wpkai辅助优化,...
wepoke辅助插件!wepo... wepoke辅助插件!wepoke透明挂辅助,wepower软件,教你攻略(有挂详情)1、不需要AI...
德扑之星作弊!线上德州辅助工具... 德扑之星作弊!线上德州辅助工具有哪些,wepower德州竟然存在有挂(详细ai人工智能教程);致您一...
wpk外 挂!wpk ai是有... wpk外 挂!wpk ai是有,Wpk一贯真的有挂,详细教程,(有挂插件);1、进入游戏-大厅左侧-...
wepoke有挂!wepoke... wepoke有挂!wepoke德州扑克(透视辅助)wepoker(一贯真的有挂);值得一提的是,we...
德扑之星猫腻!德州辅助软件线上... 德扑之星猫腻!德州辅助软件线上,德州app确实是有挂(详细作弊教程)1、很好的工具软件,可以解锁游戏...
wpk真的有外 挂!WPk本来... wpk真的有外 挂!WPk本来真的有挂,wPK其实真的有挂,黑科技教程(有挂揭秘);该软件可以轻松地...
aapoker透明挂!wepo... WePoker透视辅助工具核心要点解析‌,aapoker透明挂!wepower辅助软件,wepoKE...