/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
integration/platform-server/projects/ngmodule/src/app/transferstate/transfer-state.component.ts
37 строк
1012 B
Matthieu Riegler
build: Migrate all integration tests with the schematic. (#58160)
14 окт 2024, 17:58
14 окт 2024, 17:58
e85021a
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {isPlatformServer} from '@angular/common'; import {Component, Inject, PLATFORM_ID, TransferState, makeStateKey} from '@angular/core'; const COUNTER_KEY = makeStateKey<number>('counter'); @Component({ selector: 'transfer-state', template: ` <div>{{ counter }}</div> `, standalone: false, }) export class TransferStateComponent { counter = 0; constructor( @Inject(PLATFORM_ID) private platformId: {}, private transferState: TransferState, ) {} ngOnInit() { if (isPlatformServer(this.platformId)) { // Set it to 5 in the server. this.counter = 5; this.transferState.set(COUNTER_KEY, 50); } else { // Get the transferred counter state in the client(should be 50 and not 0). this.counter = this.transferState.get(COUNTER_KEY, 0); } } }