【发布时间】:2020-01-06 22:45:57
【问题描述】:
我不确定这里发生了什么,但我在按钮单击时调用 Vue 中的一个函数,它会进行 axios 调用,但问题是无论我在 textarea 中输入什么(v-model taskCommentBody)它将数据commentBody 发送为空白。
我在这里做错了什么?
<div class="form-group row">
<textarea v-model="taskCommentBody" class="form-control col" rows="6" placeholder="What do you have to say about this task?" name="task-comment"></textarea>
</div>
<button v-on:click="saveTaskComment" role="button" class="btn btn-primary" type="submit">
Save Comment
</button>
/**/
saveTaskComment() {
axios.post('/task/comment/save', {
commentBody: this.taskCommentBody
})
.then((response) => {
// handle success
this.comments.unshift(response.data.content);
})
.catch(function (error) {
// handle error
})
.finally(() => {
this.$root.taskCommentBody = '';
});
}
【问题讨论】:
-
如果你输入
<p>{{ taskCommentBody}}</p>,你能看到它在你改变textarea时更新吗? -
@KeithNicholas 不,它没有。我把它放在我的文本区域下,它仍然是空白的
-
在这里猜测,但您是否为
taskCommentBody定义了初始数据属性,例如data() { return { comments: [], taskCommentBody: '' } } -
@Phil 很好的猜测,因为就是这样!谢谢,如果你愿意,请继续回答,我会马上接受的
标签: javascript vue.js