当前位置: 首页 > article >正文

解锁前端表单数据的秘密旅程:从后端到用户选择!✨

😄 解锁前端表单数据的秘密旅程:从后端到用户选择!✨

嘿,技术爱好者们!👋 你有没有在开发中遇到过这样的困惑:表单里的数据(比如图片附件、识别点 ID)从哪儿来的?是后端偷偷塞给你的,还是用户辛勤上传的?🤔 今天,我们要一起踏上一场“数据探秘之旅”,以 compare-form.vue 为案例,揭开 imagesgenuineIdentificationPointIdsfakeIdentificationPointIds 这三个“宝藏”的来源秘密!🔍 准备好啦?带上你的好奇心,跟我走!🚀


🎬 开场:表单数据的“身世之谜”

想象一下,你打开 compare-form.vue,看到这样的表单组件:

<w-form-multiple-image v-model="form.images" label="图片附件" />
<w-form-select v-model="form.genuineIdentificationPointIds" :list="identifies1" />
<w-form-select v-model="form.fakeIdentificationPointIds" :list="identifies2" />

这些字段绑定了 form.images(图片数组)、genuineIdentificationPointIds(真货识别点 ID 数组)和 fakeIdentificationPointIds(假货识别点 ID 数组)。它们的数据从哪儿冒出来的?是魔法吗?🪄 别急,我们来一步步解锁!


🕵️‍♀️ 第一站:watchValue — 数据的“初始化基地”

代码中的 @Watch('value') 像是数据的“出生证明”:

@Watch('value')
watchValue(v: any) {
  this.$nextTick(async() => {
    this.form = {
      ...v,
      images: v.productPhotos ? JSON.parse(v.productPhotos) : [],
      genuineIdentificationPointIds: v.genuineIdentificationPoints ? JSON.parse(v.genuineIdentificationPoints) : [],
      fakeIdentificationPointIds: v.fakeIdentificationPoints ? JSON.parse(v.fakeIdentificationPoints) : []
    }
  })
}
  • 发现

    • value 是父组件传递的 props,v 是它的值。
    • form.imagesv.productPhotos 解析而来。
    • genuineIdentificationPointIdsv.genuineIdentificationPoints 解析。
    • fakeIdentificationPointIdsv.fakeIdentificationPoints 解析。
    • 如果没数据,默认是空数组 []
  • 推测:这些数据的“祖先”藏在 value 里,而 value 又是谁给的呢?🤔


🌍 第二站:父组件 index.vue — 数据的“中转站”

父组件是 src/views/tools/fake-strategy/index.vue,它通过 onCompare 将数据传递:

public async onCompare(row: any) {
  this.compareForm = row
  this.operateType = 'edit'
  this.compareFormVis = true
}
  • 关键

    • row 是表格一行数据,来自 this.list
    • compareForm = row 赋值给 :value="compareForm",传给 compare-form.vue
  • 数据流

    • row.productPhotosvalue.productPhotosform.images
    • row.genuineIdentificationPointsvalue.genuineIdentificationPointsform.genuineIdentificationPointIds
    • row.fakeIdentificationPointsvalue.fakeIdentificationPointsform.fakeIdentificationPointIds
  • 来源this.list 是从后端 qlist API 获取的。


🗃️ 第三站:后端 API — 数据的“老家”

index.vuegetList 揭示了终极来源:

public async getList() {
  const res: any = await qlist(this.listQuery)
  this.list = res?.data?.content
}
  • 真相

    • qlist'@/api/fake-registration' 导入。
    • res.data.content 是数组,每项 row 包含 productPhotosgenuineIdentificationPointsfakeIdentificationPoints
    • 例如:
      {
        "productPhotos": "[\"http://example.com/image1.jpg\", \"http://example.com/image2.jpg\"]",
        "genuineIdentificationPoints": "[1, 2, 3]",
        "fakeIdentificationPoints": "[4, 5]"
      }
      
  • 老家:后端数据库,通过 qlist API 提供。


🎨 第四站:动态更新 — 用户和 API 的“加成”

genuineIdentificationPointIdsfakeIdentificationPointIds 还能动态变化!通过 watchProductIdgetIdentificationPoints

@Watch('form.productId')
async watchProductId(newVal: any) {
  if (newVal) await this.getIdentificationPoints(newVal)
}

private async getIdentificationPoints(productId: number) {
  const res: any = await request({
    url: `/identificationPoint/by-product-id-and-public-type/${productId}/all`
  })
  this.identifies1 = res.data.trueIdentificationPoints.map(item => ({
    id: item.id,
    name: `${item.id}   ${item.identificationPointTitle}`
  }))
  this.identifies2 = res.data.fakeIdentificationPoints.map(item => ({
    id: item.id,
    name: `${item.id}   ${item.identificationPointTitle}`
  }))
}
  • 作用

    • 根据 form.productId 获取产品相关的识别点。
    • identifies1identifies2<w-form-select> 的选项。
    • 用户选择后更新 form.genuineIdentificationPointIdsform.fakeIdentificationPointIds
  • 来源/identificationPoint API 提供动态选项。

  • images 动态:用户通过 <w-form-multiple-image> 上传新图片,更新 form.images


🎉 总结:数据的“家族树”

  1. images

    • 初始value.productPhotos(后端 qlistrow.productPhotos)。
    • 动态:用户上传。
    • 老家:后端 productPhotos 字段。
  2. genuineIdentificationPointIds

    • 初始value.genuineIdentificationPoints(后端 qlist)。
    • 动态:用户从 identifies1 选择,选项来自 /identificationPoint
    • 老家:后端 genuineIdentificationPoints 字段 + API 选项。
  3. fakeIdentificationPointIds

    • 初始value.fakeIdentificationPoints(后端 qlist)。
    • 动态:用户从 identifies2 选择,选项来自 /identificationPoint
    • 老家:后端 fakeIdentificationPoints 字段 + API 选项。

🎨 SVG 图解:数据的旅程

数据的旅程 - SVG 优化演示
数据的旅程 后端 qlist API row 数据 index.vue 传 value compare-form 初始化 form 用户选择/上传 /identificationPoint API 🎨

🛠️ 实践建议

  • 调试console.log(value) 查初始数据,console.log(identifies1, identifies2) 看动态选项。
  • 问题:若数据空,检查 qlist/identificationPoint API 返回。
  • 优化:加错误处理,防止 JSON.parse 失败。

😂 结尾彩蛋

如果数据“失踪”,可能是后端“小哥”忘了更新数据库!😄 赶紧催一催,或者检查网络,找到“宝藏”!👀 喜欢这篇博客?欢迎留言,我下期再带你探秘!🪄


在这里插入图片描述


http://www.kler.cn/a/573435.html

相关文章:

  • 微服务通信:用gRPC + Protobuf 构建高效API
  • Java+SpringBoot+Vue+数据可视化的百草园化妆服务平台(程序+论文+讲解+安装+调试+售后)
  • 年后寒假总结及计划安排
  • linux安装Kafka以及windows安装Kafka和常见问题解决
  • 迷你世界脚本对象库接口:ObjectLib
  • Oracle CBD结构和Non-CBD结构区别
  • 微软官宣5 月 5 日关闭 Skype,赢者通吃法则依然有效
  • 解锁网络防御新思维:D3FEND 五大策略如何对抗 ATTCK
  • 如何快速的用pdfjs建立一个网页可以在线阅读你的PDF文件
  • 加密算法学习与SpringBoot实践
  • Java 多态:代码中的通用设计模式
  • 第七节:基于Winform框架的串口助手小项目---协议解析《C#编程》
  • 【数据结构初阶】---时间复杂度和空间复杂度了解及几道相关OJ题
  • Ubuntu20.04 在离线机器上安装 NVIDIA Container Toolkit
  • 【我的 PWN 学习手札】House of Emma
  • Python:简单的爬虫程序,从web页面爬取图片与标题并保存MySQL
  • Electron桌面应用开发:创建应用
  • iterm2更新后主题报错
  • 链表操作的高阶技巧:K个一组翻转链表的实现与思考
  • JVM常用概念之新对象实例化