Skip to content

Instantly share code, notes, and snippets.

@Toxicable
Last active November 22, 2017 15:33
Show Gist options
  • Save Toxicable/81619c10dc7f06417b4244cdadf71880 to your computer and use it in GitHub Desktop.
Save Toxicable/81619c10dc7f06417b4244cdadf71880 to your computer and use it in GitHub Desktop.
import { ServerTransferStateModule } from '@angular/platform-server';
@NgModule({
imports: [
AppModule,
ServerTransferStateModule,
ServerModule,
],
bootstrap: [
AppComponent,
],
})
export class AppServerModule {
}
import { TransferHttpCacheModule } from '@nguniversal/common'
import { BrowserTransferStateModule } from '@angular/platform-server';
@NgModule({
imports: [
BrowserModule.withServerTransition({appId: 'app-root'}),
BrowserTransferStateModule,
HttpClientModule,
TransferHttpCacheModule,
],
declarations: [
AppComponent,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {
}
document.addEventListener('DOMContentLoaded', () => {
platformBrowserDynamic().bootstrapModule(AppModule);
});
import { Component, PLATFORM_ID , Inject} from '@angular/core';
import { isPlatformServer } from '@angular/common'
import { TransferState , makeStateKey} from '@angular/platform-browser';
import { HttpClient } from '@angular/common/http'
interface CounterState {
count: number;
}
const COUNTER_KEY = makeStateKey<CounterState>('my-counter');
@Component({
selector: 'app-root',
template: `
{{counter | json}}
`,
styles: []
})
export class AppComponent {
counter: CounterState;
constructor(
private transferState: TransferState,
private http: HttpClient,
@Inject(PLATFORM_ID) private platformId: any,
){}
ngOnInit(){
this.transferState.onSerialize(COUNTER_KEY, () => this.counter);
this.counter = this.transferState.get(COUNTER_KEY, {count: 0});
if(isPlatformServer(this.platformId)){ //isPlatformServer check is unneeded if you use `TransferHttpCacheModule`
this.http.get('https://jsonplaceholder.typicode.com/posts/1')
.subscribe(a => ++this.counter.count)
}
}
}
@BattiniSantthosh
Copy link

BattiniSantthosh commented Nov 14, 2017

Hi Toxicable,

Am using the angular universal cli 1.0.0-alpha.universal.3,

ServerTransferStateModule, BrowserTransferStateModule and BrowserModule.withServerTransition({appId: 'app-root'}), are not importing form node-modules
Issue: angular/angular-cli#8476
Versions:

"@angular/platform-browser": "2.2.3",
"@angular/platform-browser-dynamic": "2.2.3",
"@angular/platform-server": "^2.2.3",

Can you please help me for this

@Gomathipriya
Copy link

Hi Am getting error when using

import { TransferState, makeStateKey } from '@angular/platform-browser';

@Gomathipriya
Copy link

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment