AngularUniversal的BrowserModule.withServerTransition被废弃了,什么是替代方案?
创始人
2024-10-29 21:00:35
0

替代方案是使用 TransferState 和 StateTransferInitializer 来手动管理应用程序的状态转换。这要求我们在应用程序中添加另一个 Provider, StateTransferInitializer,来管理 TransferState。

代码示例:

app.module.ts

import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { TransferState, StateTransferInitializer } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'my-app' }),
    BrowserTransferStateModule
  ],
  providers: [
    TransferState,
    StateTransferInitializer
  ],
  bootstrap: [AppComponent]
})

export class AppModule { }

app.component.ts

import { Component, OnInit } from '@angular/core';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { HttpClient } from '@angular/common/http';

const NEWS_KEY = makeStateKey('news');

@Component({
  selector: 'app-root',
  template: `
    
  • {{ newsItem.title }}
` }) export class AppComponent implements OnInit { public news: any; constructor( private http: HttpClient, private state: TransferState ) { } ngOnInit() { if (this.state.hasKey(NEWS_KEY)) { this.news = this.state.get(NEWS_KEY, null); this.state.remove(NEWS_KEY); } else { this.http.get('/api/news').subscribe(news => { this.news = news; // set the key and value in the transfer state this.state.set(NEWS_KEY, news); }); } } }

相关内容

热门资讯

外挂烘培!德普之星怎么设置埋牌... 外挂烘培!德普之星怎么设置埋牌,拱趴大菠萝万能挂图解-好像一直都是有辅助神器(哔哩哔哩)1、拱趴大菠...
外挂举措!德州机器人代打脚本,... 外挂举措!德州机器人代打脚本,智星菠萝辅助-其实是真的有辅助app(哔哩哔哩)1、起透看视 德州机器...
外挂学习!智星菠萝可以辅助吗,... 外挂学习!智星菠萝可以辅助吗,德州透视是真的假的-竟然真的是有辅助工具(哔哩哔哩)1、让任何用户在无...
外挂演示!约局吧作必弊脚本,约... 外挂演示!约局吧作必弊脚本,约局吧怎么看有没有挂-真是是有辅助攻略(哔哩哔哩)1、约局吧怎么看有没有...
外挂阶段!德州透视是真的吗,p... 外挂阶段!德州透视是真的吗,pokermaster破解版-原来真的是有辅助攻略(哔哩哔哩)进入游戏-...
外挂模块!约局吧德州有挂吗,来... 外挂模块!约局吧德州有挂吗,来玩app 德州 辅助-果然是真的有辅助脚本(哔哩哔哩)1、下载好约局吧...
外挂手筋!德扑圈透视,智星德州... 外挂手筋!德扑圈透视,智星德州插件2024最新版-竟然真的是有辅助神器(哔哩哔哩)智星德州插件202...
外挂手段!wepoker透视有... 外挂手段!wepoker透视有吗,德普之星透视-果然真的有辅助方法(哔哩哔哩)1、完成wepoker...
外挂教程书!拱趴大菠萝挂怎么安... 外挂教程书!拱趴大菠萝挂怎么安装,竞技联盟破解版最新版-切实有辅助攻略(哔哩哔哩)1、拱趴大菠萝挂怎...
外挂步骤!哈糖大菠萝可以开挂吗... 外挂步骤!哈糖大菠萝可以开挂吗,德扑圈有透视吗-本来真的有辅助攻略(哔哩哔哩)亲,关键说明,哈糖大菠...