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;

相关内容

热门资讯

黑科技线上(德扑ai工具)外挂... 黑科技线上(德扑ai工具)外挂黑科技辅助下载(透视)好像真的有挂(黑科技技巧);1、让任何用户在无需...
wpk职业代打!wpk到底有没... wpk职业代打!wpk到底有没有外挂,(Wpk)确实有挂,透明挂教程(黑科技工具);1、实时wpk到...
辅助黑科技(wpk德州ai辅助... 辅助黑科技(wpk德州ai辅助神器)外挂透视辅助软件(透视)都是真的有挂(黑科技介绍);1、下载好w...
wpk透视辅助工具的使用教程!... wpk透视辅助工具的使用教程!wpk透视辅助测试,(wpK)一贯真的是有挂,德州教程(黑科技教程)w...
黑科技辅助挂(wepoke有挂... 黑科技辅助挂(wepoke有挂吗网上靠谱吗)外挂透视辅助插件(透视)真是真的有挂(黑科技脚本)1、完...
wepoke有没有挂!wepo... wepoke有没有挂!wepoke透明挂使用方法,(Wepoke)真是有挂,插件教程(黑科技黑科技)...
黑科技讲解(德扑之星有猫腻)外... 黑科技讲解(德扑之星有猫腻)外挂透明挂辅助安装(透视)切实是真的有挂(黑科技解说)在进入德扑之星有猫...
wepoke智能ai!wepo... wepoke智能ai!wepoke透视挂靠谱,(we辅助poker)好像真的有挂,透明教程(黑科技技...
黑科技好友(aapoker有外... 黑科技好友(aapoker有外挂吗)外挂透视辅助插件(透视)都是有挂(黑科技详情)在进入aapoke...
微扑克辅助插件挂!wpkai机... 微扑克辅助插件挂!wpkai机器人,(WPK)好像真的是有挂,系统教程(黑科技软件);1、玩家可以在...