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;

相关内容

热门资讯

透视代打!wepoker代打辅... 透视代打!wepoker代打辅助,wepoker有什么规律(详细插件下载)1、让任何用户在无需wep...
透视工具!aapoker ai... 透视工具!aapoker ai插件(透视)aa poker透视软件,必胜教程(有挂解密)该软件可以轻...
透视真的!wepoker透视破... 透视真的!wepoker透视破解版,wepoker到底有没有透视(详细开辅助能查到);1)wepok...
透视讲解!aapoker ai... 透视讲解!aapoker ai插件(透视)aapoker怎么设置提高好牌几率,wpk教程(有挂方法)...
透视好友!wepokerplu... 透视好友!wepokerplus外挂,wepoker辅助器是真的吗(详细辅助插件功能)1、该软件可以...
透视透视!aapoker怎么控... 透视透视!aapoker怎么控制牌(透视)aapoker辅助插件工具,爆料教程(有挂详情);1、完成...
透视好牌!wepoker提高好... 透视好牌!wepoker提高好牌率,wepoker是不是有人用挂(详细破解辅助插件);1、wepok...
透视了解!aa poker透视... 透视了解!aa poker透视软件(透视)aapoker免费透视脚本,总结教程(有挂解密)aapok...
透视挂透视!wepoker怎么... 透视挂透视!wepoker怎么挂底牌,wepoker轻量版透视系统(详细辅助器有哪些功能)1、起透看...
透视软件!aa poker透视... 透视软件!aa poker透视软件(透视)aapoker辅助怎么用,规律教程(有挂教程)1、超多福利...