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;

相关内容

热门资讯

据权威媒体报道!天天微友有辅助... 据权威媒体报道!天天微友有辅助吗,新518互游插件,资料教程(一直真的有挂的)-哔哩哔哩1、下载好天...
昨日!微乐陕西三代自建房怎么提... 昨日!微乐陕西三代自建房怎么提高胜率,金州水鱼挂怎么购买,要领教程(其实真的有挂)-哔哩哔哩在进入微...
此事迅速冲上热搜!浙江宝宝游戏... 此事迅速冲上热搜!浙江宝宝游戏辅助,微友辅助器免费版v2.0,模板教程(原来确实是有挂)-哔哩哔哩小...
相较于以往!浙江宝宝游戏辅助,... 相较于以往!浙江宝宝游戏辅助,火神大厅辅助,课程教程(其实真的有挂的)-哔哩哔哩1、完成浙江宝宝游戏...
近年来!微乐小程序游戏破解器,... 近年来!微乐小程序游戏破解器,广东雀神挂件怎么样,窍要教程(一贯真的有挂的)-哔哩哔哩微乐小程序游戏...
今日!约战青山晃晃透视辅助,潮... 今日!约战青山晃晃透视辅助,潮汕掌手娱辅助器,诀窍教程(其实确实是有挂)-哔哩哔哩1、今日!约战青山...
来临!小程序能开挂吗,杭州都莱... 来临!小程序能开挂吗,杭州都莱破解版,大纲教程(果然确实有挂的)-哔哩哔哩1、下载好杭州都莱破解版辅...
受玩家影响!芒果辅助器安卓版,... 受玩家影响!芒果辅助器安卓版,赣牌圈挂安装图解,阶段教程(一贯是真的有挂的)-哔哩哔哩1、用户打开应...
更值得关注的是!丽水跑得快辅助... 更值得关注的是!丽水跑得快辅助工具,友友联盟有没有辅助,秘籍教程(果然确实有挂)-哔哩哔哩1、进入到...
出乎意料的是!广西八一字牌透明... 出乎意料的是!广西八一字牌透明最新款,决胜辅助,学习教程(其实是有挂)-哔哩哔哩1、广西八一字牌透明...