You could add a change
-event handler on the <input>
that uses vm.$set()
to create the new object key:
<template>
<div>
<input v-model="obj" @change="onKeyChange">
</div>
</template>
<script>
export default {
methods: {
onKeyChange() {
const key = this.obj && this.obj.trim()
if (key) {
this.$set(this.object, key, { prop1: '', prop2: '' })
}
}
}
}
</script>
The other <input>
s that depends on object[obj]
should be conditionally rendered with v-if
to avoid using prop1
/prop2
on undefined objects:
<template>
<div>
<template v-if="object[obj]">
<input v-model="object[obj]['prop1']">
<input v-model="object[obj]['prop2']">
</template>
</div>
</template>
demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…