Vue+Vuetify菜谱创建页如何实现多食材条目添加功能

这是一个支持菜谱全量管理(新增、修改、删除)的项目。当前新增菜谱环节的表单存在功能缺陷:仅能录入单条食材(每条食材需包含名称、数量两个字段),无法适配多食材录入的业务需求,需要对表单结构做针对性改造。
现有createRecipe.vue核心代码如下:
<template> <v-app> <v-container> <v-layout row> <v-flex xs12 sm6 offset-sm3> <h2 class="btn-style">Create Recipe</h2> </v-flex> </v-layout> <v-layout row> <v-flex xs12> <form @submit.prevent="onCreateRecipe"> <v-layout> <v-flex xs12 sm6 offset-sm3> <v-text-field name="id" label="Id" id="id" v-model="id" color="#43A047" required > </v-text-field> </v-flex> </v-layout> <v-layout> <v-flex xs12 sm6 offset-sm3> <v-text-field name="title" label="Title" id="title" v-model="title" color="#43A047" required > </v-text-field> </v-flex> </v-layout> <v-layout> <v-flex xs12 sm6 offset-sm3> <v-text-field name="imageUrl" label="ImageUrl" id="imageUrl" v-model="imageUrl" color="#43A047" required > </v-text-field> </v-flex> </v-layout> <v-layout> <v-flex xs12 sm6 offset-sm3> <img :src="imageUrl" height="300px"> </v-flex> </v-layout> <v-layout> <v-flex xs12 sm6 offset-sm3> <v-text-field name="description" label="Description" id="description" v-model="description" color="#43A047" multi-line required > </v-text-field> </v-flex> </v-layout> <v-layout> <v-flex xs12 sm6 offset-sm3> <v-text-field name="ingredientsName" label="IngredientsName" id="ingredientsName" v-model="ingredientsName" color="#43A047" multi-line required > </v-text-field> </v-flex> </v-layout> <v-layout> <v-flex xs12 sm6 offset-sm3> <v-text-field name="ingredientsQuantity" label="IngredientsQuantity" id="ingredientsQuantity" v-model="ingredientsQuantity" color="#43A047" multi-line required > </v-text-field> </v-flex> </v-layout> <v-layout row> <v-flex xs12 sm6 offset-sm3> <v-btn class="green darken-1 color" :disabled="!formIsValid" type="submit" > Create Recipe </v-btn> </v-flex> </v-layout> </form> </v-flex> </v-layout> </v-container> </v-app> </template> <script> export default { data() { return { id:"", title: "", imageUrl: "", description: "", ingredientsName: "", ingredientsQuantity: "", }; }, computed: { formIsValid() { return ( this.id !==""&& this.title !== "" && this.description !== "" && this.imageUrl !== "" && this.ingredientsName !==""&& this.ingredientsQuantity !== "" ); } }, methods: { onCreateRecipe() { if (!this.formIsValid) { return; } const recipeData = { id:this.id, title: this.title, description: this.description, imageUrl: this.imageUrl, ingredientsName: this.ingredientsName, ingredientsQuantity: this.ingredientsQuantity }; // 此处调用setter将数据存入对应位置 this.$store.commit('createRecipe', recipeData) const stringifiedData = JSON.stringify(recipeData); localStorage.setItem("recipe", stringifiedData); console.log("We got : ", JSON.parse(localStorage.getItem("recipe"))); } } }; </script> <style scoped> .btn-style { color: #43a047; } .color { color: #fafafa; } </style>
改造方案
核心思路是把原来单字符串绑定的两个独立食材字段,替换为结构化的数组类型食材列表,通过动态增删表单项实现多食材录入能力,具体改动如下:
- 调整响应式状态:删除原来独立的
ingredientsName、ingredientsQuantity单值变量,替换为数组类型的食材列表,初始值默认包含1条空食材,保证页面加载时默认显示1行可填写的食材录入项
data() { return { id: "", title: "", imageUrl: "", description: "", // 替换原有单值字段 ingredients: [ { name: "", quantity: "" } ] } }
- 改造模板结构:把原来固定的两个食材输入框,替换为循环渲染的动态食材行,每行搭配对应字段的输入框,非首行提供删除按钮,同时新增“添加食材”的操作入口
<!-- 替换原有固定的食材输入块 --> <v-flex xs12 sm6 offset-sm3> <div v-for="(ing, index) in ingredients" :key="index" class="ingredient-row"> <v-text-field :name="`ingredientsName_${index}`" label="食材名称" v-model="ing.name" color="#43A047" required ></v-text-field> <v-text-field :name="`ingredientsQuantity_${index}`" label="食材数量" v-model="ing.quantity" color="#43A047" required ></v-text-field> <v-btn v-if="index > 0" color="red darken-1" small @click="removeIngredient(index)" > 删除 </v-btn> </div> <v-btn color="green darken-1" small class="mb-3" @click="addIngredient" > + 添加一条食材 </v-btn> </v-flex>
- 新增操作逻辑:
- 实现
addIngredient、removeIngredient方法,完成食材行的动态增删 - 调整
formIsValid校验规则:除了校验基础字段非空,还要保证所有食材行的名称、数量字段都不为空 - 提交数据时直接把结构化的
ingredients数组存入提交对象,不再拆分独立的名称、数量字段
- 实现
methods: { addIngredient() { this.ingredients.push({ name: "", quantity: "" }) }, removeIngredient(index) { this.ingredients.splice(index, 1) }, onCreateRecipe() { if (!this.formIsValid) return const recipeData = { id: this.id, title: this.title, description: this.description, imageUrl: this.imageUrl, ingredients: this.ingredients } this.$store.commit('createRecipe', recipeData) localStorage.setItem("recipe", JSON.stringify(recipeData)) } }, computed: { formIsValid() { const baseValid = this.id !== "" && this.title !== "" && this.description !== "" && this.imageUrl !== "" if (!baseValid) return false return this.ingredients.every(ing => ing.name.trim() !== "" && ing.quantity.trim() !== "") } }
优化细节
- 可在样式中增加食材行的布局调整,避免输入框挤压:
.ingredient-row { margin-bottom: 16px; display: flex; gap: 12px; align-items: flex-end; } - 后续对接菜谱编辑功能时,只需把接口返回的食材数组直接赋值给响应式的
ingredients变量,即可自动完成所有食材的回显,无需额外做数据拆分转换
内容的提问来源于stack exchange,提问作者Hiba RRRR
相关产品推荐
相关产品推荐

