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;

相关内容

热门资讯

经核实!红龙poker辅助器免... 经核实!红龙poker辅助器免费观看,德普之星透视软件免费入口官网,确实真的有挂(真的有挂)德普之星...
总算明白!wepoker安装教... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
截至发稿!哈糖大菠萝能开挂吗,... 截至发稿!哈糖大菠萝能开挂吗,智星菠萝透视,好像存在有挂(有挂方针)1、智星菠萝透视免费脚本咨询教程...
最新技巧!wepoker是不是... 最新技巧!wepoker是不是有人用挂,wepoker底牌透视,果然真的有挂(有人有挂)运辅助工具,...
备受关注的!impoker辅助... 备受关注的!impoker辅助,德普之星怎么开辅助,总是真的有挂(有挂技巧)1、实时德普之星怎么开辅...
重大发现!wepoker好友助... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
此事迅速冲上热搜!wepoke... 此事迅速冲上热搜!wepoker透视脚本免费app,德普之星透视辅助软件是真的吗,切实真的有挂(有挂...
大神推荐!wepoker网页版... 大神推荐!wepoker网页版透视方法,wepoker-h5下载,竟然是有挂(有挂猫腻)辅助器是一种...
针对!wepoker私人局外卦... 针对!wepoker私人局外卦,德普之星透视辅助软件是真的吗,本来真的是有挂(果真有挂)1、每一步都...
总算清楚!wepoker免费透... 总算清楚!wepoker免费透视脚本,约局吧透视挂下载,都是是有挂(有挂实锤)1、任何wepoker...