gpt4 book ai didi

redux - 如何使用 ngFor 以角度获取和显示 ngrx 存储数据

转载 作者:行者123 更新时间:2023-12-04 15:44:18 25 4
gpt4 key购买 nike

我正在状态数组中存储表单数据。我正在接收数组,但它是嵌套形式。我不知道如何显示它。

//查看Viewcomponent.ts

customerarray: Customer[];
ngOnInit() {
// this.customerObs = this.store.select('customerList');
this.store.select<Customer[]>('customerList').subscribe(res =>
{
this.customerarray = res;
console.log(res);
console.log(this.customerarray);
});
}

//viewcomponent.html

<li *ngFor="let customer of customerarray; i as index">
<span>{{ i + 1}}.</span> {{customer.customer.name}}
</li>

//reducer.ts

import { Customer } from '../app/models/customer';
import { ADD_CUSTOMER } from '../app/store/action';
import * as CustomerAction from '../app/store/action';
const initialState = {
customer: [
new Customer('Steve', 'Yellow'),
new Customer('RDJ', 'Red')
]
};

export function CustomerReducer(state = initialState, action: CustomerAction.AddCustomer) {
console.log(state.customer);
switch (action.type) {
case CustomerAction.ADD_CUSTOMER:
return {`enter code here`

...state,
customer: [...state.customer, action.payload]
};

default:
return state;
}
}

最佳答案

我认为这是一个变化检测问题。您的组件不会在此订阅上呈现。

试试这个 -

.ts 文件 -

customersObs:Observable<Customer[]>
constructor() {
this.customersObs = this.store.select<Customer[]>('customerList');
}

.html 文件 -

<li *ngFor="let customer of cusomersObs | async; i as index">
<span>{{ i + 1}}.</span> {{customer.name}}
</li>

关于redux - 如何使用 ngFor 以角度获取和显示 ngrx 存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56544398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com