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;

相关内容

热门资讯

必备透视!德州辅助工具到底怎么... 必备透视!德州辅助工具到底怎么样!总是是真的有辅助神器(有挂技巧)-哔哩哔哩1、进入游戏-大厅左侧-...
解迷透视!wpk辅助哪里买,w... 解迷透视!wpk辅助哪里买,wepoker免费脚本咨询,经验教程(真实有挂)-哔哩哔哩1、该软件可以...
解谜透视!聚星ai辅助工具激活... 解谜透视!聚星ai辅助工具激活码!真是是有辅助app(证实有挂)-哔哩哔哩该软件可以轻松地帮助玩家将...
专业透视!wpk透视工作室,w... 专业透视!wpk透视工作室,wepoker辅助是真的吗,举措教程(有挂详情)-哔哩哔哩1、wepok...
教你透视!德州局怎么透视!果然... 教你透视!德州局怎么透视!果然真的是有辅助工具(有挂细节)-哔哩哔哩1、德州局怎么透视公共底牌简单,...
详情透视!如何下载wepoke... 详情透视!如何下载wepoker安装包,wepoker透视有没有,秘籍教程(果真有挂)-哔哩哔哩1、...
推荐透视!we poker免费... 推荐透视!we poker免费辅助器!都是是真的有辅助app(有挂教学)-哔哩哔哩1、让任何用户在无...
解密透视!约局吧怎么看有没有挂... 解密透视!约局吧怎么看有没有挂,红龙poker辅助器免费观看,方案教程(有挂方略)-哔哩哔哩;1、每...
详情透视!红龙poker有辅助... 详情透视!红龙poker有辅助吗!竟然真的是有辅助插件(有挂方略)-哔哩哔哩1、操作简单,无需红龙p...
有挂透视!hhpoker到底可... 有挂透视!hhpoker到底可以作必弊吗,德州局hhpoker,阶段教程(有挂规律)-哔哩哔哩1、h...