我有以下模板:
<template>
<div class="is-half">
<form @submit.prevent="save">
<input type="hidden" name="bookID" :value="book.id">
<div class="field">
<label class="label">Title</label>
<div class="control">
<input class="input" type="text" placeholder="Title" :value="book.title">
</div>
</div>
<div class="control">
<div class="select">
<select>
<option
v-for="author in this.$store.state.authors"
:value="author.name"
:selected="author.name == book.author"
>{{ author.name }}</option>
</select>
</div>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<textarea class="textarea" placeholder="Description" :value="book.description"></textarea>
</div>
</div>
<div class="control">
<button class="button is-primary">Submit</button>
</div>
</form>
</div>
</template>
<script>
export default {
data() {
return {
book : {
id: null,
title: '',
isbn: '',
author: '',
description: '',
added: ''
}
}
},
methods: {
save(book) {
console.log(this.book);
}
},
created() {
if(this.$store.state.book != 'undefined'){
this.book = Object.assign({}, this.$store.state.book);
}
},
computed: {}
}
</script>
<style></style>
我正在尝试更新所选项目的值,但每当我按下save时,该对象具有与加载时相同的值。 如果我加载新对象,或者如果 id 为空,则如何插入新对象,我该如何更新值?
答案 0 :(得分:4)
如果我理解你的问题,问题是当你在输入中输入内容时,它不会更新模型。
问题是您使用:value
来绑定值,这是一种单向绑定。对于双向绑定,将所有:value
替换为v-model
:v-model="book.title"