Dynamically obtain data before page display
Click the "Get Dynamic Data" button -> The page will be displayed after the data is dynamically obtained
Enter data in the input box -> click the "Print input data" button
code example
< template>
< div>
< el-input
v-for = " (item, index) in arr"
:key = " index"
v-model = " dataObj['index' + index]"
@change = " changeHandler"
/>
< el-button type = " primary" @click = " arr = ['aaa', 'bbb', 'ccc']" > 获取动态数据</ el-button>
< el-button type = " primary" @click = " gethandler" > 打印input数据</ el-button>
</ div>
</ template>
< script>
export default {
name : 'Ceshi' ,
components : {
} ,
data ( ) {
return {
arr : [ ] ,
dataObj : {
}
}
} ,
computed : {
} ,
watch : {
arr : {
handler ( val ) {
val. forEach ( ( item, index ) => {
this . $set ( this . dataObj, 'index' + index, '' )
} )
}
}
} ,
created ( ) {
} ,
methods : {
gethandler ( ) {
console. log ( '获取 dataObj 数据 ----' , this . dataObj)
} ,
changeHandler ( ) {
console. log ( ' change事件 this.dataObj----' , this . dataObj)
}
}
}
</ script>
< style lang = ' scss' scoped >
</ style>
problems encountered
When adding attributes to objects, this.dataObj['index' + index] = ''
it is not feasible, but this.$set(this.dataObj, 'index' + index, '')
it is feasible