Created
October 2, 2017 01:20
-
-
Save battlecow/60c08f2b31fefa5fa82b8fdac2c5c3d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Inject, Component, OnInit, Injector} from '@angular/core'; | |
import {PLATFORM_ID} from '@angular/core'; | |
import {isPlatformBrowser, isPlatformServer} from '@angular/common'; | |
import {TransferState, StateKey, makeStateKey} from '@angular/platform-browser'; | |
const COUNTER_KEY = makeStateKey<number>('counter'); | |
@Component({ | |
template: ` | |
{{counter}} | |
`, | |
styles: [] | |
}) | |
export class InstanceDetailComponent implements OnInit { | |
public counter: number = 0; | |
constructor(private transferState: TransferState, private injector: Injector, @Inject(PLATFORM_ID) private platformId: Object) { | |
} | |
ngOnInit() { | |
if (isPlatformServer(this.platformId)) { | |
// Set it to 5 in the server. | |
this.counter = 5; | |
this.transferState.set(COUNTER_KEY, 50); | |
console.log(this.transferState); | |
} else { | |
console.log(this.transferState); | |
// Get the transferred counter state in the client(should be 50 and not 0). | |
this.counter = this.transferState.get(COUNTER_KEY, 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment