【发布时间】:2022-01-11 10:54:37
【问题描述】:
这是组件类例如:
export class AppComponent {
categories = {
country: [],
author: []
}
constructor(){}
getOptions(options) {
options.forEach(option => {
const key = option.name;
this.categories[key].push(option.value);
})
}
}
点击按钮时,我从不同的组件调用getOptions(options)。选项的结构如下:
options = [
{name: 'country', value: 'Germany'},
{name: 'author', value: 'Franz Kafka'}
]
所以现在this.categories 的值将会更新,所以现在:
this.categories[country] = ["Germany"]
this.categories[author] = ["Frank Kafka"]
每次单击按钮时,options 的值都会发生变化。当我发送新的options 值时,例如:
options = [
{name: 'country', value: 'Japan'},
{name: 'author', value: 'Masashi Kishimoto'}
]
this.categories[country] 的旧值由于某种原因没有得到保存。 this.categories[country] 的新值应该是 ["Germany, "Japan"],但数组中只有 ["Japan"]。
【问题讨论】:
-
您是否在代码中的任何位置将 this.categories[country](或 [author])设置为 [ ]?
-
@Mishi Mashina 不,虽然只有声明,但我将它们设置为 [ ],而不是其他任何地方。
标签: javascript angular typescript