Skip to content
On this page

Angular

Use @nanostores/angular and NanostoresService with useStore() method to get store’s value and subscribe for store’s changes.

// NgModule:
import { NANOSTORES, NanostoresService } from '@nanostores/angular';
@NgModule({
providers: [{ provide: NANOSTORES, useClass: NanostoresService }]
})
// Component:
import { Component } from '@angular/core'
import { NanostoresService } from '@nanostores/angular'
import { Observable, switchMap } from 'rxjs'
import { profile } from '../stores/profile'
import { IUser, User } from '../stores/user'
@Component({
selector: 'app-root',
template: '<p *ngIf="(currentUser$ | async) as user">{{ user.name }}</p>'
})
export class AppComponent {
currentUser$: Observable<IUser> = this.nanostores
.useStore(profile)
.pipe(switchMap(userId => this.nanostores.useStore(User(userId))))
constructor(private nanostores: NanostoresService) {}
}

Imported from the Nano Stores README for version 1.5.5. Improve this page there.