3 回答

TA貢獻1853條經驗 獲得超9個贊
許多內置組件(包括 forEach)都包含一個可選的“this”活頁夾:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
利用這一點來發(fā)揮你的優(yōu)勢:
this.inputList.forEach(function (element) { predictionParams[element.detail] = this.form[element.detail] },this)
自 ie9 起受支持

TA貢獻1808條經驗 獲得超4個贊
arrow 函數語法避免了重新綁定此
this.inputList.forEach(element => { predictionParams[element.detail] = this.form[element.detail] })

TA貢獻1803條經驗 獲得超3個贊
您可以使用箭頭函數 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions 它將它綁定到函數中
data () {
return {
question: [],
inputList: [],
form: {}
}
},
methods: {
onSubmit: () => {
let predictionParams = {}
this.inputList.forEach((element) => {
predictionParams[element.detail] = this.form[element.detail]
})
}
添加回答
舉報