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
@State({ | |
name: 'counter', | |
defaults: 0 | |
}) | |
@Injectable() | |
class CounterState implements NgxsOnChanges, NgxsOnInit, NgxsAfterBootstrap { | |
public ngxsOnChanges(): void { | |
// .. | |
} |
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
@State<AuthModel>({ | |
name: 'auth', | |
defaults: { | |
token: null, | |
exp: null | |
} | |
}) | |
@Injectable() | |
export class AuthState { | |
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
@StateRepository() | |
@State<AuthModel>({ | |
name: 'auth', | |
defaults: { | |
token: null, | |
exp: null | |
} | |
}) | |
@Injectable() | |
export class AuthState extends NgxsDataRepository<AuthModel> { } |
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
@Injectable() | |
export class TokenInterceptor { | |
constructor(private authInfo: AuthState) {} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
if (this.authInfo.snapshot.token) { | |
req = req.clone({ | |
setHeaders: { | |
Authorization: `Bearer ${this.authInfo.snapshot.token}`, | |
}, |
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
@Injectable() | |
export class TokenInterceptor { | |
constructor(private store: Store) {} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
const token: string | null = this.store.selectSnapshot(AuthState.token); | |
if (token) { | |
req = req.clone({ | |
setHeaders: { |
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
interface UserModel { | |
id: number | null; | |
username: string | null; | |
age: number | null; | |
} | |
@StateRepository() | |
@State<UserModel>({ | |
name: 'userInfo', | |
defaults: { |
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
interface UserModel { | |
id: number | null; | |
username: string | null; | |
age: number | null; | |
} | |
export class UserSetAction { | |
public static type: string = '[UserSetAction]: action'; | |
constructor(public payload: UserModel) {} | |
} |
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
// ... | |
@Component({ | |
selector: 'counter', | |
template: ` | |
value: {{ (counter$ | async).value }} <br> | |
<button (click)="increment()">increment</button> | |
` | |
}) | |
export class CounterComponent { |
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 { Injectable } from '@angular/core'; | |
import { State } from '@ngxs/store'; | |
interface CountModel { | |
value: number; | |
} | |
export class IncrementAction { | |
public static type: string = '[Increment]'; | |
constructor(public value: number) {} |
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 { Injectable } from '@angular/core'; | |
import { Store } from './store'; | |
interface CountModel { | |
value: number; | |
} | |
@Injectable() | |
export class CounterStore extends Store<CountModel> { | |
private initialCount: CountModel = { value: 0 }; |