以下是一篇技术博客,主题围绕 @Watch('form.productId')
和 @Watch('value')
这两个 watcher 的功能、区别及使用场景,基于 compare-form.vue
的代码。准备好一起探索 Vue 监听器的魔法了吗?
嘿,Vue 开发者们! 你有没有在项目中遇到过“数据变化时该怎么处理”的困惑?特别是在动态表单中,监听器(@Watch
)就像守门员,决定什么时候该“放行”数据操作。今天,我们要以 compare-form.vue
为案例,深入剖析 @Watch('form.productId')
和 @Watch('value')
这两个监听器的区别和作用,带你解锁 Vue 监听的秘密! 准备好啦?带上好奇心,跟我走!
在 Vue.js(尤其是 TypeScript 项目)中,@Watch
装饰器让我们可以监控数据的变化,执行自定义逻辑。compare-form.vue
中有两个关键的 watcher:
@Watch('form.productId')
async watchProductId(newVal: any) {
if (newVal) {
await this.getIdentificationPoints(newVal)
} else {
this.identifies1 = []
this.identifies2 = []
}
}
@Watch('value')
watchValue(v: any) {
this.$nextTick(async() => {
this.getProducts()
if (v.productId) {
await this.getIdentificationPoints(v.productId)
}
this.form = {
...v,
images: v.productPhotos ? JSON.parse(v.productPhotos) : [],
genuineIdentificationPointIds: v.genuineIdentificationPoints ? JSON.parse(v.genuineIdentificationPoints) : [],
fakeIdentificationPointIds: v.fakeIdentificationPoints ? JSON.parse(v.fakeIdentificationPoints) : []
}
})
}
它们听起来都跟 productId
和数据初始化有关,但到底有什么不同?咱们一探究竟!
@Watch('form.productId')
— 动态守门员form.productId
form
对象中的 productId
,绑定到
,由用户选择“涉及产品”更新。productId
值时。@Watch('value')
— 初始化守门员监听属性:value
index.vue
传递的 compareForm
数据。触发时机:父组件更新 value
(比如调用 onCompare
)时,通常在组件加载或数据刷新时。
角色:初始化表单数据和预加载相关选项。
区别亮点:
form.productId
是“内部玩家”,随用户操作变化。value
是“外部使者”,由父组件驱动。watchProductId
的动态魔法@Watch('form.productId')
async watchProductId(newVal: any) {
if (newVal) {
await this.getIdentificationPoints(newVal)
} else {
this.identifies1 = []
this.identifies2 = []
}
}
form.productId
变化(newVal
是新值),调用 getIdentificationPoints(newVal)
从 /identificationPoint API
获取真假识别点。newVal
为空,清空 identifies1
和 identifies2
(下拉选项)。 和 :list="identifies2"
的选项。
productId = 123
,getIdentificationPoints(123)
填充选项。watchValue
的初始化魔法@Watch('value')
watchValue(v: any) {
this.$nextTick(async() => {
this.getProducts()
if (v.productId) {
await this.getIdentificationPoints(v.productId)
}
this.form = {
...v,
images: v.productPhotos ? JSON.parse(v.productPhotos) : [],
genuineIdentificationPointIds: v.genuineIdentificationPoints ? JSON.parse(v.genuineIdentificationPoints) : [],
fakeIdentificationPointIds: v.fakeIdentificationPoints ? JSON.parse(v.fakeIdentificationPoints) : []
}
})
}
功能:
getProducts
),如果 v.productId
存在,预加载识别点。form
,解析 value
中的图片和识别点数据。效果:
productList
填充
。v.productId
有效,identifies1
和 identifies2
预填。form
准备好展示预设值。目的:组件启动时,快速准备好表单和选项。
区别亮点:
watchProductId
是“动态调整”,随用户操作。watchValue
是“一次性初始化”,随父组件。watchProductId
productId
)。productId = 123
改成 456
,重新加载选项。watchValue
频率:通常一次(组件加载或父数据更新)。
场景:组件初始化或编辑已有记录。
例子:打开弹窗,value
传 { productId: 123 }
,预填充数据。
区别亮点:
watchProductId
是“持续服务”,watchValue
是“启动仪式”。来看个直观的 SVG,展示两者的区别:
Vue 监听器的魔法之旅 - SVG 优化演示探索 @Watch('form.productId') 和 @Watch('value') 的区别!
watchValue 初始化数据,watchProductId 动态调整!
console.log('watchValue:', v)
和 console.log('watchProductId:', newVal)
跟踪触发。newVal: number
提高类型安全。getIdentificationPoints
失败,检查网络或 API 返回。如果 watchProductId
不动,可能是用户忘了选产品! 或者 watchValue
跑太快,API 还没醒,赶紧 console.log
抓“懒虫”! 喜欢这篇?留言告诉我,下期见!