Created
July 3, 2018 19:49
-
-
Save roelofjan-elsinga/763eddfcd6e2948b9135286081c83da3 to your computer and use it in GitHub Desktop.
Subscribing to an action in Angular 6
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 {Component, OnInit} from '@angular/core'; | |
import {NgRedux, select} from "@angular-redux/store"; | |
import {IAppState} from "./path/to/state"; | |
@Component({ | |
selector: 'app-any', | |
templateUrl: './any.component.html', | |
styleUrls: ['./any.component.scss'] | |
}) | |
export class AnyComponent implements OnInit { | |
@select() languages; | |
constructor(private ngRedux: NgRedux<IAppState>) { } | |
ngOnInit() { | |
//@angular-redux/store provides the @select decorator, | |
//so you can listen to a change in a specific reducer | |
this.languages.subscribe(language => { | |
console.log(languages); | |
//Will display: {shortcode:"en",name:"English",english_name:"English"} | |
}); | |
//You can also use NgRedux to listen to the whole state for changes if you want | |
this.ngRedux.subscribe(() => { | |
let state = this.ngRedux.getState(); | |
console.log(state.languages); | |
//Will also display: {shortcode:"en",name:"English",english_name:"English"} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment