[英]Why is my *ngFor reacting to ngrx state changes but my subscribe method does only once
在我的應用程序中,我有一個 ngrx 存儲,其中包含一組對象。 在我的構造函數中,我選擇了數組並調用了一個方法 (onProductEntriesLoaded),該方法為該數組設置了一個變量。 在 html 代碼中有一個 *ngFor 循環,它遍歷這個變量並顯示它們。 設置變量的方法也會調用另一個計算總和的方法。 但是,每次在狀態中向此數組添加元素時,內容都會在 *ngFor 循環中正確顯示。 但是沒有調用將變量設置為數組的方法。 它僅在初始化時調用。 我認為訂閱狀態的應用程序的每個部分都會在狀態發生變化時再次加載。 即使它是一個構造函數。 然而,它似乎並非如此。 有人可以向我解釋這種行為。
這是我的 cart.component.html 的一部分
<div id="middle">
<div class="entries">
<div *ngFor="let entry of productEntries">
<img
*ngIf="!entry.product.image"
src="../../../assets/img/esansBottles Kopie.png"
/>
<img
*ngIf="entry.product.image"
src="../../../assets/img/{{ entry.product.image }}"
/>
{{ entry.product.title }}
{{ entry.variation.option }}
{{ entry.variation.price }}
{{ entry.amount }}
</div>
</div>
</div>
<div id="bottom">
<div>Summe {{ sumOfCart }} €</div>
<button>Zur Kasse</button>
</div>
這基本上就是cart.component.ts文件
export class CartComponent implements OnInit {
productEntries: ShoppingCartEntry[];
sumOfCart: number;
constructor(private store: Store<State>) {
this.store
.select(state => state.shoppingCartReducer.Entries)
.subscribe(data => this.onProductEntriesLoaded(data));
}
ngOnInit() {}
onProductEntriesLoaded(productEntries: ShoppingCartEntry[]) {
console.log("onProductEntreisLoaded called");
this.productEntries = productEntries;
console.log(productEntries);
console.log("next calculate sum");
this.calculateSum(productEntries);
console.log("after calculate sum");
}
calculateSum(shoppingCartEntries: ShoppingCartEntry[]) {
console.log("calculate sum called");
/*let sum = 0.0;
console.log(shoppingCartEntries);
if (shoppingCartEntries) {
shoppingCartEntries.forEach(element => {
sum = element.amount * element.variation.price + sum;
});
}
this.sumOfCart = sum;*/
}
這是我的 reducer.ts 文件的一部分
export function shoppingCartReducer(
state: ShoppinCartState = { CartIsOpen: false, Entries: [] },
action: ShoppingCartAction
) {
switch (action.type) {
case "CART_TOGGLE":
return {
...state,
CartIsOpen: !state.CartIsOpen
};
case "CART_CLOSE":
return {
...state,
CartIsOpen: false
};
case "CART_ADD_ENTRY":
return {
...state,
Entries: addEntryToCart(state.Entries, action.payload)
};
default:
return state;
}
}
function addEntryToCart(
currentEntries: ShoppingCartEntry[],
entry: ShoppingCartEntry
): ShoppingCartEntry[] {
//if currentEntries is null return empty array
if (currentEntries) {
// if entry is null return currentEntries
if (entry) {
// check if currentEntries contains entry
let index = currentEntries.findIndex(
x => x.product === entry.product && x.variation === entry.variation
);
if (index === -1) {
let newEntries = currentEntries;
newEntries.push(entry);
return newEntries;
} else {
currentEntries[index].amount += entry.amount;
return currentEntries;
}
} else {
return currentEntries;
}
} else {
return [];
}
}
問題是由我的減速器功能引起的。 在我的減速器中刪除對 addEntryToCart 的函數調用並在減速器中實現功能后,問題就解決了。 我仍然不明白為什么調用函數會導致問題。
我就是這樣解決的:
export function shoppingCartReducer(
state: ShoppinCartState = { CartIsOpen: false, Entries: [] },
action: ShoppingCartAction
) {
switch (action.type) {
case "CART_TOGGLE":
return {
...state,
CartIsOpen: !state.CartIsOpen
};
case "CART_CLOSE":
return {
...state,
CartIsOpen: false
};
case "CART_ADD_ENTRY":
let contains = state.Entries.findIndex(
entry =>
entry.product === action.payload.product &&
entry.variation === action.payload.variation
);
if (contains === -1) {
return {
...state,
Entries: [...state.Entries.concat(action.payload)]
};
} else {
return {
...state,
Entries: [
...state.Entries.map(entry =>
entry.product === action.payload.product &&
entry.variation === action.payload.variation
? {
product: entry.product,
variation: entry.variation,
amount: entry.amount + action.payload.amount
}
: entry
)
]
};
}
default:
return state;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.