据我了解,Redux 主要是为了在 javascript 应用程序中启用双向数据绑定。这对于不是双向数据绑定的框架非常有用,例如 React。但是为什么要在 Angular 中使用它,它本来就已经是双向数据绑定了?
为了说明我的问题,我在本机 Angular 中使用的代码创建了一个存储,以便在两个 Angular 组件之间进行可变状态的通信:
1)商店
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class StoreService {
customer: any;
constructor() { }
}
Run Code Online (Sandbox Code Playgroud)
这家商店是一个原生的 Angular 服务,我只声明可变客户(是的,打字会更好,但我想尽可能地缩短它)。
2)异步API SERVIC ë
import { Injectable } from '@angular/core';
import { StoreService } from './store.service'
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor( private store: StoreService ) { }
getData() {
setTimeout(()=> {
this.store.customer = {
name: 'Bob',
age: 25
}
}, 2000); …Run Code Online (Sandbox Code Playgroud)