Javascript 在满足条件时运行功能?
我正在使用Vuex构建一个应用程序,当我的数据对象满足一些用户定义的条件时,我不确定如何运行函数Javascript 在满足条件时运行功能?,javascript,vue.js,Javascript,Vue.js,我正在使用Vuex构建一个应用程序,当我的数据对象满足一些用户定义的条件时,我不确定如何运行函数 <template> <swiper-slide v-for="(photon,key) in this.$store.state.photons" :key='key'> <ul> <li><label for="tempAlert">Temp exceeds</label><input v-model="p
<template>
<swiper-slide v-for="(photon,key) in this.$store.state.photons" :key='key'>
<ul>
<li><label for="tempAlert">Temp exceeds</label><input v-model="photon.user.tempAlert" type="number"></li>
<li><datetime type='datetime' v-model="photon.user.alertTime" use12-hour auto class="theme-orange">
<label for="alertTime" slot="before">Notify at</label></datetime></li>
</ul>
</swiper-slider>
<template>
<script>
methods:{
photonAlert(photon) {
if(
photon.user.alertTime>=new Date() &&
photon.user.tempAlert>=photon.data.tempF[photon.data.tmepF.length-1][1]
) {
this.sendnotification(photon)
}
},
}
</script>
- 温度超过
-
通知
方法:{
光子探测器(光子){
如果(
photon.user.alertTime>=新日期()&&
photon.user.tempAlert>=photon.data.tempF[photon.data.tmepF.length-1][1]
) {
此.sendnotification(光子)
}
},
}
当条件满足时,监视更改和运行功能的正确方法是什么?Vue有一个功能,可以执行此操作,
watch
:
watch: {
photon: function (photon) {
if(...
},
正如Jeff在评论中指出的,您正在处理一个对象,可能需要使用deep:true
来捕获更改:
watch: {
photon: {
handler: function (photon) { if(... },
deep: true
}
},
您尝试过吗?如果这是一个对象,您可能还需要添加
deep:true
。然后函数将位于对象的处理程序属性下。