admin管理员组文章数量:1023273
I'm trying to emit a prop that I modify. I can do that directly with the prop and it works but I get a Vue Warn: Avoid mutating a prop directly since the value will be overwritten whenever the parent ponent re-renders
So I tried to put the prop in a puted, but the emit executes before the puted is executed.
template:
<input
v-on:keyup="validatedate(localDate)"
v-on:change="emitAnswer(localDate)"
v-model="localDate">
,
puted: {
dateLocal: {
get: function () {
return this.date
}
}
methods: {
emitAnswer: function (date) {
this.$emit('myFunc', date);
}
}
I'm trying to emit a prop that I modify. I can do that directly with the prop and it works but I get a Vue Warn: Avoid mutating a prop directly since the value will be overwritten whenever the parent ponent re-renders
So I tried to put the prop in a puted, but the emit executes before the puted is executed.
template:
<input
v-on:keyup="validatedate(localDate)"
v-on:change="emitAnswer(localDate)"
v-model="localDate">
,
puted: {
dateLocal: {
get: function () {
return this.date
}
}
methods: {
emitAnswer: function (date) {
this.$emit('myFunc', date);
}
}
Share
Improve this question
asked Apr 8, 2019 at 13:43
AmarAmar
5292 gold badges17 silver badges37 bronze badges
3 Answers
Reset to default 5Since Vue can't guarantee that v-model will be executed before the v-on:change, you can use watchers to only call the emit once the value of the property has been changed in the ponent, example:
watch {
date() {
this.$emit('myFunc', this.date)
}
}
Firstly, I believe your puted is named wrongly ("dateLocal" should be "localDate"), but I thing that is not the problem.
Why don´t you check inside method right before emit if value is set?
methods: {
emitAnswer: function (date) {
if (date) {
this.$emit('myFunc', date)
}
}
}
Also, for better practise, you should use set
handler for puted property you are about to modify:
puted: {
localDate: {
get () {
return this.date
},
set (date) {
if (date) {
this.$emit('myFunc', date)
}
}
}
}
Hopefully this helps you, feel free to respond if you need further help.
To avoid mutating a prop, it’s best to define a local data property that uses the prop as its initial value (source):
props: ['date'],
data () {
return {
localDate: this.date
}
}
Now you can use the localDate
data as v-model
and you don't need any parameter for your emitAnswer
method:
//template
<input @change="emitAnswer" v-model="localDate">
//script
props: ['date'],
data () {
return {
localDate: this.date
}
},
methods: {
emitAnswer () {
this.$emit('myFunc', this.localDate)
}
}
I'm trying to emit a prop that I modify. I can do that directly with the prop and it works but I get a Vue Warn: Avoid mutating a prop directly since the value will be overwritten whenever the parent ponent re-renders
So I tried to put the prop in a puted, but the emit executes before the puted is executed.
template:
<input
v-on:keyup="validatedate(localDate)"
v-on:change="emitAnswer(localDate)"
v-model="localDate">
,
puted: {
dateLocal: {
get: function () {
return this.date
}
}
methods: {
emitAnswer: function (date) {
this.$emit('myFunc', date);
}
}
I'm trying to emit a prop that I modify. I can do that directly with the prop and it works but I get a Vue Warn: Avoid mutating a prop directly since the value will be overwritten whenever the parent ponent re-renders
So I tried to put the prop in a puted, but the emit executes before the puted is executed.
template:
<input
v-on:keyup="validatedate(localDate)"
v-on:change="emitAnswer(localDate)"
v-model="localDate">
,
puted: {
dateLocal: {
get: function () {
return this.date
}
}
methods: {
emitAnswer: function (date) {
this.$emit('myFunc', date);
}
}
Share
Improve this question
asked Apr 8, 2019 at 13:43
AmarAmar
5292 gold badges17 silver badges37 bronze badges
3 Answers
Reset to default 5Since Vue can't guarantee that v-model will be executed before the v-on:change, you can use watchers to only call the emit once the value of the property has been changed in the ponent, example:
watch {
date() {
this.$emit('myFunc', this.date)
}
}
Firstly, I believe your puted is named wrongly ("dateLocal" should be "localDate"), but I thing that is not the problem.
Why don´t you check inside method right before emit if value is set?
methods: {
emitAnswer: function (date) {
if (date) {
this.$emit('myFunc', date)
}
}
}
Also, for better practise, you should use set
handler for puted property you are about to modify:
puted: {
localDate: {
get () {
return this.date
},
set (date) {
if (date) {
this.$emit('myFunc', date)
}
}
}
}
Hopefully this helps you, feel free to respond if you need further help.
To avoid mutating a prop, it’s best to define a local data property that uses the prop as its initial value (source):
props: ['date'],
data () {
return {
localDate: this.date
}
}
Now you can use the localDate
data as v-model
and you don't need any parameter for your emitAnswer
method:
//template
<input @change="emitAnswer" v-model="localDate">
//script
props: ['date'],
data () {
return {
localDate: this.date
}
},
methods: {
emitAnswer () {
this.$emit('myFunc', this.localDate)
}
}
本文标签: javascriptemit executes before computedStack Overflow
版权声明:本文标题:javascript - $emit executes before computed - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745597685a2158267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论