/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
integration/platform-server/projects/standalone/src/app/transferstate/transfer-state.component.ts
39 строк
1 KB
Joey Perrott
refactor: update license text to point to angular.dev (#57901)
24 сен 2024, 16:33
24 сен 2024, 16:33
9dbe6fc
Код
Авторство
О чём код?
/** * @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 {HttpClient} from '@angular/common/http'; import {Component, Inject, PLATFORM_ID, TransferState, makeStateKey} from '@angular/core'; const COUNTER_KEY = makeStateKey<number>('counter'); @Component({ selector: 'transfer-state', standalone: true, template: ` <div>{{ counter }}</div> `, providers: [HttpClient], }) 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); } } }