2 回答

TA貢獻1856條經驗 獲得超17個贊
最后,我找到了另一個解決方案,我從輸入中獲取值并將參數傳遞給 api url
代碼:
在模板中輸入:
<input v-on:keyup.enter="addTodo(categorie.id)" type="text" class="form-control" v-bind:id="categorie.id" aria-describedby="name" placeholder="Add a todo">
函數:addTodo()
addTodo(categorie_id) {
let val = document.getElementById(categorie_id).value
fetch(`api/todo/${categorie_id}/${val}`, {
method: 'post',
body: JSON.stringify(this.todo),
headers: {
'content-type': 'application/json'
}
})
.then(res => res.json())
.then(data => {
this.clearInput(categorie_id);
this.fetchTodos();
this.fetchCategories();
})
.catch(err => console.log(err));
},
以及清除輸入的功能:
clearInput(categorie_id) {
document.getElementById(categorie_id).value = '';
}

TA貢獻1828條經驗 獲得超3個贊
您的數據中應該有另一個屬性可在輸入字段中使用,例如 form: { name: '' } 并在您的輸入 v-model="form.name" 上將此屬性用作 v-model
添加回答
舉報