AngularUniversalSSR在初始导航后不能使用包含参数的路由
创始人
2024-10-29 21:01:13
0

这个问题很可能是由于Angular在初始导航后无法处理URL参数导致的。解决此问题的方法是使用ServerTransferStateModule来传递状态,从而使Angular Universal能够正确地处理带有参数的路由。

以下是一些示例代码: 在你的app.module.ts中添加以下代码:

import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { TransferState } from '@angular/platform-browser';

@NgModule({
  imports: [
    BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
    FormsModule,
    HttpClientModule,
    AppRoutingModule,
    BrowserTransferStateModule
  ],
  declarations: [
    AppComponent
  ],
  providers: [
    TransferState
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后在你的组件中添加以下代码:

import { Component, OnInit, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { makeStateKey, TransferState } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';

const SOME_STATE_KEY = makeStateKey('some_state_key');

@Component({
  selector: 'app-some-component',
  templateUrl: './some-component.component.html',
  styleUrls: ['./some-component.component.css']
})
export class SomeComponent implements OnInit {

  someValue: string;

  constructor(
    private route: ActivatedRoute,
    private transferState: TransferState,
    @Inject(PLATFORM_ID) private platformId
    ) { }

  ngOnInit() {
    if (isPlatformBrowser(this.platformId)) {
      // We are running on the client side, so get the state (if any) and clear it
      const state = this.transferState.get(SOME_STATE_KEY, null);
      this.transferState.set(SOME_STATE_KEY, null);

      if (state !== null) {
        this.someValue = state;

相关内容

热门资讯

黑科技教程(wePoKe)软件... 黑科技教程(wePoKe)软件透明挂是真的吗(黑科技)有挂(总是存在有挂)是一款可以让一直输的玩家,...
黑科技总结(德州之星有透视挂吗... 黑科技总结(德州之星有透视挂吗)外挂透明挂辅助插件(透视)切实真的有挂(黑科技详情)1、黑科技总结(...
黑科技安装(WePoKer)计... 自定义系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分享给你好...
黑科技中牌率(wepoke透明... 黑科技中牌率(wepoke透明挂操作技巧)外挂黑科技辅助挂(透视)总是有挂(黑科技解密)wepoke...
透视智能ai(WpK)透视辅助... 透视智能ai(WpK)透视辅助有病毒吗(黑科技)德州透视外挂(都是是真的有挂)是一款可以让一直输的玩...
黑科技能赢(德州ai辅助软件)... 黑科技能赢(德州ai辅助软件)外挂透视辅助挂(透视)都是是真的有挂(黑科技辅助)德州ai辅助软件辅助...
黑科技挂(wepOkE)辅助挂... 黑科技挂(wepOkE)辅助挂(黑科技)透明黑科技(竟然存在有挂)1、金币登录送、破产送、升级送、活...
黑科技ai代打(wpk有外挂吗... 黑科技ai代打(wpk有外挂吗)外挂透视辅助软件(透视)确实真的是有挂(黑科技细节)1、wpk有外挂...
了解辅助(微扑克)ai软件(辅... 了解辅助(微扑克)ai软件(辅助挂)ai会被检测到(一直真的是有挂)是一款可以让一直输的玩家,快速成...
黑科技科技(wepoke透明挂... 黑科技科技(wepoke透明挂操作技巧)外挂黑科技辅助软件(透视)竟然真的是有挂(黑科技技巧)wep...