admin管理员组文章数量:1312778
So I'm trying to build a ponent in Vue 3 that acts as a form, and in order for the data to be processed by the parent I want it to emit an object with all the inputs on change. The issue I'm having is that I don't seem to be able to call $emit
from within watch()
(probably because of the context, but I also don't see why the ponent-wide context isn't passed by default, and it doesn't accept this
). I also cannot call any method because of the same reason.
I do see some people using the watch: {}
syntax but as I understand it that is deprecated and it doesn't make a whole lot of sense to me either.
Here's a minimal example of what I'm trying to acplish. Whenever the input date is changed, I want the ponent to emit a custom event.
<template>
<input
v-model="date"
name="dateInput"
type="date"
>
</template>
<script>
import { watch, ref } from "vue";
export default {
name: 'Demo',
props: {
},
emits: ["date-updated"],
setup() {
const date = ref("")
watch([date], (newVal) => {
// $emit is undefined
console.log(newVal);
$emit("date-updated", {newVal})
// watchHandler is undefined
watchHandler(newVal)
})
return {
date
}
},
data() {
return {
}
},
mounted() {
},
methods: {
watchHandler(newVal) {
console.log(newVal);
$emit("date-updated", {newVal})
}
},
}
</script>
So I'm trying to build a ponent in Vue 3 that acts as a form, and in order for the data to be processed by the parent I want it to emit an object with all the inputs on change. The issue I'm having is that I don't seem to be able to call $emit
from within watch()
(probably because of the context, but I also don't see why the ponent-wide context isn't passed by default, and it doesn't accept this
). I also cannot call any method because of the same reason.
I do see some people using the watch: {}
syntax but as I understand it that is deprecated and it doesn't make a whole lot of sense to me either.
Here's a minimal example of what I'm trying to acplish. Whenever the input date is changed, I want the ponent to emit a custom event.
<template>
<input
v-model="date"
name="dateInput"
type="date"
>
</template>
<script>
import { watch, ref } from "vue";
export default {
name: 'Demo',
props: {
},
emits: ["date-updated"],
setup() {
const date = ref("")
watch([date], (newVal) => {
// $emit is undefined
console.log(newVal);
$emit("date-updated", {newVal})
// watchHandler is undefined
watchHandler(newVal)
})
return {
date
}
},
data() {
return {
}
},
mounted() {
},
methods: {
watchHandler(newVal) {
console.log(newVal);
$emit("date-updated", {newVal})
}
},
}
</script>
Share
Improve this question
edited Mar 22, 2021 at 20:55
Boussadjra Brahim
1
asked Mar 22, 2021 at 20:35
JuckixJuckix
351 gold badge3 silver badges7 bronze badges
2 Answers
Reset to default 4Don't mix between option and position api in order to keep the ponent consistent, the emit
function is available in the context
parameter of the setup
hook::
<template>
<input
v-model="date"
name="dateInput"
type="date"
>
</template>
<script>
import { watch, ref } from "vue";
export default {
name: 'Demo',
props: {},
emits: ["date-updated"],
setup(props,context) {// or setup(props,{emit}) then use emit directly
const date = ref("")
watch(date, (newVal) => {
context.emit("date-updated", {newVal})
})
return {
date
}
},
}
</script>
if you want to add the method watchHandler
you could define it a plain js function like :
...
watch(date, (newVal) => {
context.emit("date-updated", {newVal})
})
function watchHandler(newVal) {
console.log(newVal);
context.emit("date-updated", {newVal})
}
...
How should I do this in setup syntactic sugar
const emits = defineEmits(['confirmCallBack', 'cancelCallBack', 'closeCallBack', 'openCallBack', 'update:modelValue'])
const dialogForm = ref<CommonForm >({})
watch(dialogForm.value, (val) => {
consola.start('数据更新')
emits('update:modelValue', val)
})
本文标签: javascriptVue 3call emit on variable changeStack Overflow
版权声明:本文标题:javascript - Vue 3, call $emit on variable change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741883519a2402888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论