Last active
February 28, 2021 03:34
-
-
Save arturovt/8acd858c4a92d119c6d01c6a82ffb547 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
// This will be used on the client-side | |
@Injectable({ providedIn: 'root' }) | |
export class ArticleService { | |
constructor(private transferState: TransferState) {} | |
getArticle(id: string): Observable<Article> { | |
const key = makeStateKey<Article>(`article:${id}`); | |
const article = this.transferState.get(key, null!); | |
return of(article); | |
} | |
} | |
// This will be used on the server-side | |
@Injectable() | |
export class ServerArticleService { | |
constructor(private http: HttpClient, private transferState: TransferState) {} | |
getArticle(id: string): Observable<Article> { | |
return this.http.get<Article>(`${environment.apiUrl}/api/article/${params.id}`).pipe( | |
tap(article => { | |
const key = makeStateKey<Article>(`article:${id}`); | |
this.transferState.set(key, article); | |
}), | |
); | |
} | |
} | |
@NgModule({ | |
providers: [ | |
{ | |
provide: ArticleService, | |
useClass: ServerArticleService, | |
}, | |
], | |
}) | |
export class AppServerModule {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment