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;

相关内容

热门资讯

第9分钟了解!大宝江苏麻将有挂... 第9分钟了解!大宝江苏麻将有挂(辅助挂)起初是真的有挂(专业辅助攻略教程)-哔哩哔哩;大宝江苏麻将有...
两分钟了解!天天南通长牌有挂(... 两分钟了解!天天南通长牌有挂(辅助挂)切实是真的有挂(专业辅助必备教程)-哔哩哔哩;详细天天南通长牌...
第1分钟了解!陕麻圈辅助软件(... 第1分钟了解!陕麻圈辅助软件(辅助挂)一直真的是有挂(专业辅助扑克教程)-哔哩哔哩是一款可以让一直输...
9分钟了解!老友广东麻将有挂(... 9分钟了解!老友广东麻将有挂(辅助挂)都是是真的有挂(专业辅助wpk教程)-哔哩哔哩;1、让任何用户...
第8分钟了解!科乐吉林麻将胡牌... 第8分钟了解!科乐吉林麻将胡牌规律(辅助挂)先前是真的有挂(专业辅助技巧教程)-哔哩哔哩是一款可以让...
5分钟了解!情怀app麻将可以... 您好:情怀app麻将可以控牌这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
第二分钟了解!广西星悦字牌有挂... 您好,广西星悦字牌有挂么这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家...
九分钟了解!众汇棋牌有挂(辅助... 九分钟了解!众汇棋牌有挂(辅助挂)真是存在有挂(专业辅助爆料教程)-哔哩哔哩;1、点击下载安装,众汇...
七分钟了解!钱塘十三水辅助器下... 七分钟了解!钱塘十三水辅助器下载(辅助挂)最初真的有挂(专业辅助技巧教程)-哔哩哔哩是一款可以让一直...
第2分钟了解!同城游牛鬼有挂(... 第2分钟了解!同城游牛鬼有挂(辅助挂)一贯是有挂(专业辅助科技教程)-哔哩哔哩;亲真的是有正版授权,...