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;

相关内容

热门资讯

重大通报“哥哥游戏辅助神器”h... 重大通报“哥哥游戏辅助神器”hhpoker德州牛仔视频(素来真的有挂)1、许多玩家不知道哥哥游戏辅助...
黑科技辅助挂!智星菠萝德州辅助... 黑科技辅助挂!智星菠萝德州辅助器工具,智星德州辅助器免费下载,攻略方法(有挂透明挂)1、智星菠萝德州...
关于“潮友潮汕木虱辅助”wep... 关于“潮友潮汕木虱辅助”wepoker可以免费玩吗(真是是有挂)1、点击下载安装,微扑克wpk插件透...
黑科技辅助挂!(wePoKe)... 黑科技辅助挂!(wePoKe)都是真的有挂,(wepoKe)总是真的是有挂,德州论坛(有挂透视)德州...
实测发现“闲逸辅助软件下载”竞... 实测发现“闲逸辅助软件下载”竞技联盟透视插件(起初真的是有挂);人气非常高,ai更新快且高清可以动的...
黑科技辅助挂!(we-poke... 黑科技辅助挂!(we-poker)总是真的有挂,(WEpoke)确实是有挂,AI教程(有挂辅助器);...
技术分享“决战13水辅助”哈糖... 技术分享“决战13水辅助”哈糖大菠萝挂法(先前是有挂);大神普及一款德州ai内幕,确定了哈糖大菠萝挂...
黑科技辅助挂!智星德州辅助器工... 黑科技辅助挂!智星德州辅助器工具,智星德州辅助器推荐,攻略方法(有挂辅助挂)黑科技辅助挂!智星德州辅...
我来教大家“胡乐辅助脚本有哪些... 我来教大家“胡乐辅助脚本有哪些常见功能”aa poker辅助包(其实有挂);致您一封信;亲爱胡乐辅助...
黑科技辅助挂!(AAPOkER... 黑科技辅助挂!(AAPOkER)好像存在有挂,(AAPoker)本来真的有挂,软件教程(有挂脚本);...