/ / दुकान से राज्य कैसे लेते हैं? कोणीय / ngrx - कोणीय, ngrx

दुकान से राज्य कैसे लेते हैं? कोणीय / ngrx - कोणीय, ngrx

मैं लंबे समय से प्रतिक्रिया / redux के साथ काम करते हैं और अब मैं कोणीय / ngrx सीखने की कोशिश कर रहा हूँ।

और मेरे पास सवाल थे

दुकान में कैसे लिखते हैं? रेडक्स स्टोर में मैं बस प्रयुक्त फ़ंक्शन कनेक्ट करता हूं कनेक्ट करें, और कोणीय में मैं इस तरह से कनेक्ट करता हूं

        @Component({   selector: "home",
templateUrl: "./home.html" }) export class HomeComponent implements OnInit {

console = console;
todos$: Observable<any>;
todo: string;
todoDate: string;
indexToEdit: number | null;


constructor(private store: Store<any>) {}

ngOnInit() {
this.todos$ = this.store.select("todoReducer");
}

अगले मैं उपयोग करता हूँ todos$ टेम्पलेट में चक्र के लिए, लेकिन अगर मैं console.log(todos$) इसकी दुकान की बस वस्तु है, और मैं अपने स्टोर की स्थिति नहीं ढूंढ सकता, मैं स्टोर से राज्य कैसे पढ़ सकता हूं?

उत्तर:

उत्तर № 1 के लिए 1

आप rxjs ऑपरेटर का उपयोग कर सकते हैं कर दुष्प्रभाव दिखाने के लिए इसे आयात करना न भूलें।

this.todos$ = this.store.select("todoReducer").do(res => console.log(res))

एक और तरीका सब्सक्राइब करना होगा लेकिन यह एनजीआरएक्स के उद्देश्य को हरा देगा। और आप टेम्पलेट में एसिंक पाइप का उपयोग नहीं करेंगे और आपको सदस्यता समाप्त करने की आवश्यकता होगी।

storeSub: Subscription;

this.storeSub = this.store.select("todoReducer").subscribe((data) => {
console.log(data);
this.todos = data;
})

उत्तर № 2 के लिए 1

todo $ आपके राज्य का एक देखने योग्य है।

आप इसे इस तरह नियंत्रक में उपयोग कर सकते हैं:

this.todo$.subscribe(state => console.log(state));

या इस तरह अपने टेम्पलेट में:

{{ todo$ | async }}
<input type="text" name="myfield" (change)="modifyField($event.target.value)" [value]="(todo$ | async)?.myfield"></input>