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、蘑菇云游破解版ai机器...
第六分钟解迷!兴动辅助器,新5... 第六分钟解迷!兴动辅助器,新518互游插件下载(果然真的是有挂)-哔哩哔哩1)新518互游插件下载辅...
第3分钟外挂!决战卡五星辅助修... 第3分钟外挂!决战卡五星辅助修改器,福建天天开心辅助器真的假的,2025新版(有挂秘籍)-哔哩哔哩;...
1分钟揭露!微信开心泉州辅助,... 1分钟揭露!微信开心泉州辅助,新九五辅助(竟然存在有挂)-哔哩哔哩微信开心泉州辅助辅助器中分为三种模...
1分钟外挂!乐酷有透视吗,牛总... 1分钟外挂!乐酷有透视吗,牛总管辅助器,我来教教你(有挂细节)-哔哩哔哩1、每一步都需要思考,不同水...
第9分钟必备!闲来山水广西辅助... 第9分钟必备!闲来山水广西辅助,四川熊猫辅助软件下载(真是真的有挂)-哔哩哔哩四川熊猫辅助软件下载辅...
第4分钟外挂!游戏茶苑辅助器,... 第4分钟外挂!游戏茶苑辅助器,雀友会广东潮汕bus,高科技教程(有挂讲解)-哔哩哔哩在进入雀友会广东...
一分钟普及!小程序牵手跑得有开... 一分钟普及!小程序牵手跑得有开挂吗,闲逸辅助脚本(一直存在有挂)-哔哩哔哩1)小程序牵手跑得有开挂吗...
第8分钟外挂!爱来510k辅助... 第8分钟外挂!爱来510k辅助器,开心泉州辅助,微扑克教程(今日头条)-哔哩哔哩运爱来510k辅助器...
5分钟专业!微信小程序挂件辅助... 5分钟专业!微信小程序挂件辅助,微友助手怎么破解(一贯真的有挂)-哔哩哔哩1、每一步都需要思考,不同...