把任务"校对目录"改成"校对附录",点保存,接口却报错了。这时列表应继续显示哪个标题?如果输入框直接绑定列表里的对象,用户刚打完字,列表就已经变了;即使请求失败,页面看起来仍像保存成功。本篇给上节的任务应用补上写入流程,并让失败可以稳定复现。
1. 把表单和列表的职责分开
列表保存最近一次确认的记录;表单只保存正在编辑的编号和标题。点编辑时,把标题复制进输入框。提交成功才替换列表记录,失败就保留输入值和编号。创建也走这个入口,区别只是编号由谁分配。筛选直接读已确认的列表,因此一个尚未保存的改名不会把任务提前移出筛选结果。

2. 做一个可以主动触发失败的页面
在Vue 3与Vite项目中新建TaskBoard.vue,把它作为上节列表路由的组件即可;单独练习时也可以直接用以下代码替换App.vue。示例使用内存数组和模拟保存函数,每次刷新都会回到初始记录。"让下次保存失败"只影响下一次提交,便于核对重试时有没有重复创建。
vue
<script setup>
import { computed, ref } from 'vue'
const rows = ref([{ id: 1, title: '校对目录' }])
const editing = ref(null)
const draft = ref('')
const query = ref('')
const busy = ref(false)
const error = ref('')
const failNext = ref(false)
let nextId = 2
const visible = computed(() =>
rows.value.filter(row => row.title.includes(query.value.trim()))
)
function edit(row) {
editing.value = row.id
draft.value = row.title
error.value = ''
}
function reset() {
editing.value = null
draft.value = ''
error.value = ''
}
async function persist(id, title) {
await new Promise(resolve => setTimeout(resolve, 250))
if (failNext.value) {
failNext.value = false
throw new Error('模拟保存失败')
}
return { id: id ?? nextId++, title }
}
async function save() {
const title = draft.value.trim()
if (!title || busy.value) return
busy.value = true
error.value = ''
try {
const saved = await persist(editing.value, title)
if (editing.value === null) rows.value.push(saved)
else rows.value = rows.value.map(row => row.id === saved.id ? saved : row)
reset()
} catch (cause) {
error.value = cause.message
} finally {
busy.value = false
}
}
</script>
<template>
<label>筛选 <input v-model="query"></label>
<ul><li v-for="row in visible" :key="row.id">
{{ row.title }} <button :disabled="busy" @click="edit(row)">编辑</button>
</li></ul>
<form @submit.prevent="save">
<fieldset :disabled="busy">
<legend>{{ editing === null ? '新建任务' : '编辑任务' }}</legend>
<label>标题 <input v-model="draft"></label>
<label><input type="checkbox" v-model="failNext">让下次保存失败</label>
<button :disabled="!draft.trim()">{{ error ? '重试保存' : '保存' }}</button>
<button type="button" @click="reset">取消</button>
</fieldset>
<p role="status">{{ busy ? '保存中' : error }}</p>
</form>
</template>
3. 用筛选条件暴露提前提交的问题
输入筛选词"目录",编辑第一条,把标题改成"校对附录"。勾选失败开关后保存,预期列表仍显示"校对目录",输入框保留"校对附录",并出现重试按钮。再点一次,保存成功后,这一行才从当前筛选视图消失。清空筛选条件,应看到改名后的那一条记录。
接着新建"检查图片",再复现一次失败和重试。这次成功后总数应从一条变成两条。两条用例分别检查按编号替换和追加;只看成功提示,会漏掉"更新时多出一条"的错误。
4. 接真实接口时的两个边界
示例的失败发生在模拟服务分配编号之前,因此重试创建是确定的。真实服务可能已经写入,只有响应在途中丢失。对这种不确定结果,服务端应支持稳定的请求标识与去重,或让客户端查询写入结果后再决定是否重发。
busy限制同一组件中的重复提交。两个人同时编辑同一任务时,还需要服务端按版本检查更新。接入上节的Pinia时,把成功后的替换操作放进store,保留表单草稿为页面本地状态,两页才能观察到同一份已确认的数据。
5. 思维导图
#mermaid-svg-4iNuJ0KJydT7LLFJ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-4iNuJ0KJydT7LLFJ .error-icon{fill:#552222;}#mermaid-svg-4iNuJ0KJydT7LLFJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4iNuJ0KJydT7LLFJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .marker.cross{stroke:#333333;}#mermaid-svg-4iNuJ0KJydT7LLFJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4iNuJ0KJydT7LLFJ p{margin:0;}#mermaid-svg-4iNuJ0KJydT7LLFJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .cluster-label text{fill:#333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .cluster-label span{color:#333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .cluster-label span p{background-color:transparent;}#mermaid-svg-4iNuJ0KJydT7LLFJ .label text,#mermaid-svg-4iNuJ0KJydT7LLFJ span{fill:#333;color:#333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .node rect,#mermaid-svg-4iNuJ0KJydT7LLFJ .node circle,#mermaid-svg-4iNuJ0KJydT7LLFJ .node ellipse,#mermaid-svg-4iNuJ0KJydT7LLFJ .node polygon,#mermaid-svg-4iNuJ0KJydT7LLFJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-4iNuJ0KJydT7LLFJ .rough-node .label text,#mermaid-svg-4iNuJ0KJydT7LLFJ .node .label text,#mermaid-svg-4iNuJ0KJydT7LLFJ .image-shape .label,#mermaid-svg-4iNuJ0KJydT7LLFJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-4iNuJ0KJydT7LLFJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-4iNuJ0KJydT7LLFJ .rough-node .label,#mermaid-svg-4iNuJ0KJydT7LLFJ .node .label,#mermaid-svg-4iNuJ0KJydT7LLFJ .image-shape .label,#mermaid-svg-4iNuJ0KJydT7LLFJ .icon-shape .label{text-align:center;}#mermaid-svg-4iNuJ0KJydT7LLFJ .node.clickable{cursor:pointer;}#mermaid-svg-4iNuJ0KJydT7LLFJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .arrowheadPath{fill:#333333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-4iNuJ0KJydT7LLFJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4iNuJ0KJydT7LLFJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-4iNuJ0KJydT7LLFJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4iNuJ0KJydT7LLFJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-4iNuJ0KJydT7LLFJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-4iNuJ0KJydT7LLFJ .cluster text{fill:#333;}#mermaid-svg-4iNuJ0KJydT7LLFJ .cluster span{color:#333;}#mermaid-svg-4iNuJ0KJydT7LLFJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-4iNuJ0KJydT7LLFJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-4iNuJ0KJydT7LLFJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-4iNuJ0KJydT7LLFJ .icon-shape,#mermaid-svg-4iNuJ0KJydT7LLFJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4iNuJ0KJydT7LLFJ .icon-shape p,#mermaid-svg-4iNuJ0KJydT7LLFJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-4iNuJ0KJydT7LLFJ .icon-shape .label rect,#mermaid-svg-4iNuJ0KJydT7LLFJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4iNuJ0KJydT7LLFJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-4iNuJ0KJydT7LLFJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-4iNuJ0KJydT7LLFJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 编辑草稿
提交快照
保存成功
替换或追加记录
重新筛选
保存失败
保留输入再提交
总结要点
要在错误发生后继续编辑,输入框就需要一份独立的草稿。把列表更新放在保存成功分支,筛选、详情和错误提示才能对同一次操作给出一致的反馈。
验收创建看条数,验收更新看编号,验收失败看旧记录和新输入是否同时保留。这三个观察点比单看提示文字更能发现数据错误。
参考Vue表单输入绑定。
下一篇用组件行为测试固定这些结果,再构建生产版,验证直接打开详情链接的表现。
👉 如果你觉得这篇文章对你有所帮助,欢迎点赞、收藏、分享!😊