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

相关内容

热门资讯

黑科技了解!(德州)外挂辅助工... 黑科技了解!(德州)外挂辅助工具,(德州之星)原本是有挂,2025新版总结(有挂辅助);1分钟了解详...
wpk发牌逻辑!wpk透视辅助... wpk发牌逻辑!wpk透视辅助可测试真的,德州aiapp,玩家教程(原生存在有挂);玩家必备必赢加哟...
黑科技代打!(AApOKER)... 黑科技代打!(AApOKER)透明挂辅助机制,(aapokEr)起初是真的有挂,玩家教程(有挂攻略)...
wepoke透明挂使用教程!微... 1、wepoke透明挂使用教程!微扑克机制技巧,gg扑克实战,玩家教程(都是真的是有挂);详细教程。...
黑科技工具!(aapOker)... 黑科技工具!(aapOker)透明挂辅助挂,(aapOKER)先前有挂,力荐教程(有挂透明);最新版...
微扑克辅助透视技能教程!wpk... 微扑克辅助透视技能教程!wpk透视辅助效果图,wpk微扑克有辅助吗,必胜教程(原来真的是有挂);建议...
黑科技玄学!(wepoke)软... 1、黑科技玄学!(wepoke)软件透明挂,(wopoker)从前存在有挂,科技教程(有挂辅助);详...
wepoke有没有挂!wpk职... wepoke有没有挂!wpk职业代打,wepoke辅助有挂吗,细节方法(都是真的是有挂)1、点击下载...
黑科技透明挂!(AApOKER... 黑科技透明挂!(AApOKER)透明挂辅助插件,(AaPOKER)原生是有挂,必胜教程(有挂方法)1...
wepoke辅助挂!wepok... wepoke辅助挂!wepoke有吗,德扑之星数据解析,力荐教程(本来是真的有挂);支持多人共享记分...