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

相关内容

热门资讯

黑科技插件(智星德州菠萝偷偷看... 黑科技插件(智星德州菠萝偷偷看牌功能)外挂黑科技辅助挂(透视)确实真的有挂(黑科技技巧)1、每一步都...
黑科技ai代打(wepoke插... 黑科技ai代打(wepoke插件)外挂黑科技辅助助手(透视)确实真的是有挂(黑科技细节)1、打开软件...
黑科技美元局(wepoke软件... 黑科技美元局(wepoke软件透明挂)外挂透明挂辅助挂(透视)一直是真的有挂(黑科技方法)1、游戏颠...
黑科技游戏(Wpk有挂吗)外挂... 黑科技游戏(Wpk有挂吗)外挂透明挂辅助方法(透视)其实存在有挂(黑科技脚本);1、很好的工具软件,...
黑科技教程(微扑克辅助工具怎么... 黑科技教程(微扑克辅助工具怎么下载)外挂透明挂辅助神器(透视)原来真的有挂(黑科技解密)1)微扑克辅...
黑科技系统(红龙扑克机制)外挂... 黑科技系统(红龙扑克机制)外挂黑科技辅助插件(透视)确实是有挂(黑科技插件)1、红龙扑克机制系统规律...
辅助黑科技(WPK透视辅助)外... 辅助黑科技(WPK透视辅助)外挂透明挂辅助教程(透视)果然是真的有挂(黑科技解密);1、下载好WPK...
黑科技智能ai(wepoke插... 黑科技智能ai(wepoke插件)外挂黑科技辅助软件(透视)真是是有挂(黑科技解密)小薇(透视辅助)...
黑科技实锤(德州ai代打)外挂... 黑科技实锤(德州ai代打)外挂透明挂辅助下载(透视)一直存在有挂(黑科技辅助);1、德州ai代打系统...
黑科技脚本(微扑克辅助工具怎么... 黑科技脚本(微扑克辅助工具怎么下载)外挂黑科技辅助教程(透视)其实有挂(黑科技攻略)1、这是跨平台的...