[英]Running a function when conditions are met?
I'm building an application using Vuex and i'm not sure how to run a function when my data object meets a few user defined conditions. 我正在使用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="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>
What is the proper way to go about watching for changes and running a function when conditions are met? 满足条件时,观察更改和运行功能的正确方法是什么?
Vue has a facility for this, watch
: Vue为此提供了一个设施,请
watch
:
watch: {
photon: function (photon) {
if(...
},
As Jeff pointed out in comments, you are handling an object and may need to use deep: true
to catch changes: 正如Jeff在评论中指出的那样,您正在处理一个对象,可能需要使用
deep: true
来捕获更改:
watch: {
photon: {
handler: function (photon) { if(... },
deep: true
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.