Tengo el siguiente código, quiero obtener y setear una variable global en mi aplicacion angular con NGXS pero no logro hacerlo, siempre me retorna undefined. tengo el siguiente codigo:
Estado
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { SetGarita } from './garita-actions';
import { garitaStateModel } from './garita-model';
import { Injectable } from '@angular/core';
@State<garitaStateModel>({
name: 'garita',
defaults: {
id: "",
}
})
@Injectable()
export class GaritaState {
@Selector()
static getGarita(state: garitaStateModel) {
return state.id;
}
@Action(SetGarita)
set({ getState, patchState }: StateContext<garitaStateModel>, { garita }: SetGarita) {
const state = getState();
patchState({
id: garita
}
);
}
}
modelo
export class garitaStateModel {
id: string;
}
action
export class SetGarita {
static readonly type = '[Garita] Set';
constructor( public garita: string ) {}
}
componente
//para setear mi variable (funciona)
this.store.dispatch(new SetGarita("23"));
//no me funciona
this.garita$ = this.store.select(GaritaState.getGarita);
this.garita$.subscribe(garita => {
console.log(garita)
})