admin管理员组文章数量:1202577
I would like to create a function to reformat date fields without the (time) part.
- old value (1950-01-01 00:00:00)
- New value (1950-01-01).
but I can't manage to reassign this New value to my Porps: DataAgent.date_naissance.
I get this error message: ESLint: Unexpected side effect in computed function. (vue/no-side-effects-in-computed-properties).
Can you help me?
Thank you very much.
Child component :
<div class="col-5 champ-dialog">
<span class="Libelle-Titre">Date de naissance </span>
<span v-if="ActionForm === variablesGlobals.create || ActionForm ===
variablesGlobals.modification" class="required-marker">*</span>
<q-input
dense
:disable="DisableDateNaissance"
clearable
clear-icon="close"
:color="DataFormColor"
type="date"
v-model="DataAgent.date_naissance"
lazy-rules
:rules="[dateNaissNullRule, dateNaissRule]"/>
</div>
Props:
const props = defineProps({
BlocAgent: Object,
FormColor: String,
ActionForm: String
})
const DataAgent = reactive(props.BlocAgent)
Function :
const FormatedDateNaissance = computed(() => {
const OldDate = DataAgent.date_naissance.split(' ')
Eror HERE => DataAgent.date_naissance.value = OldDate[0]
console.log(DataAgent.date_naissance.value)
return DataAgent.date_naissance.value
})
console.log = 1950-01-01
return de laravel
I would like to create a function to reformat date fields without the (time) part.
- old value (1950-01-01 00:00:00)
- New value (1950-01-01).
but I can't manage to reassign this New value to my Porps: DataAgent.date_naissance.
I get this error message: ESLint: Unexpected side effect in computed function. (vue/no-side-effects-in-computed-properties).
Can you help me?
Thank you very much.
Child component :
<div class="col-5 champ-dialog">
<span class="Libelle-Titre">Date de naissance </span>
<span v-if="ActionForm === variablesGlobals.create || ActionForm ===
variablesGlobals.modification" class="required-marker">*</span>
<q-input
dense
:disable="DisableDateNaissance"
clearable
clear-icon="close"
:color="DataFormColor"
type="date"
v-model="DataAgent.date_naissance"
lazy-rules
:rules="[dateNaissNullRule, dateNaissRule]"/>
</div>
Props:
const props = defineProps({
BlocAgent: Object,
FormColor: String,
ActionForm: String
})
const DataAgent = reactive(props.BlocAgent)
Function :
const FormatedDateNaissance = computed(() => {
const OldDate = DataAgent.date_naissance.split(' ')
Eror HERE => DataAgent.date_naissance.value = OldDate[0]
console.log(DataAgent.date_naissance.value)
return DataAgent.date_naissance.value
})
console.log = 1950-01-01
return de laravel
Share Improve this question edited Jan 22 at 6:17 DarkBee 15.7k8 gold badges70 silver badges114 bronze badges asked Jan 21 at 14:32 Kaddour FellahKaddour Fellah 11 bronze badge 3 |1 Answer
Reset to default 0As the ESLint error says: your computed
has side-effect which means it does some updating and mutates the component state. It may introduce an infinite loop. The doc says:
It is considered a very bad practice to introduce side effects inside computed properties and functions. It makes the code not predictable and hard to understand.
You can simply avoid this by separating the computation and the mutation blocks.
import { computed, reactive, watch } from 'vue';
const props = defineProps({
BlocAgent: Object,
FormColor: String,
ActionForm: String
})
const DataAgent = reactive(props.BlocAgent)
const FormatedDateNaissance = computed(() => {
const OldDate = DataAgent.date_naissance.split(' ');
return OldDate[0];
});
watch(FormatedDateNaissance, () => {
DataAgent.date_naissance.value = FormatedDateNaissance.value;
});
Here, compute the FormatedDateNaissance
value without changing the state, then watch for changes and proceed.
A similar question in Vue2.
Update full working code:
<!-- child component -->
<template>
<q-input
type="date"
:model-value="dateOnly"
@update:model-value="dateChanged"
/>
</template>
<script setup lang="ts">
import { computed } from 'vue'
defineProps({
formColor: {
type: String,
},
actionForm: {
type: String,
},
})
// define a sync property.
const blocAgent = defineModel<{ date_naissance: string }>('blocAgent', {
default: () => ({ date_naissance: '' }),
required: true,
})
// always separate the date part only
const dateOnly = computed(() => blocAgent.value.date_naissance.split(' ')?.[0] || '')
// sync and update the model after separating date parts
function dateChanged(val: string | number | null) {
blocAgent.value.date_naissance = (val?.toString() || '').split(' ')[0]!
}
</script>
And call in the parent component:
<!-- parent -->
<template>
<child-component
form-color="primary"
action-form="action-form"
v-model:bloc-agent="blockAgent"
/>
<div>date_naissance: {{ blockAgent.date_naissance }}</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import ChildComponent from 'components/ChildComponent.vue'
const blockAgent = ref({
date_naissance: '1950-01-01 00:00:00',
})
</script>
I removed code that is not related to the question for brevity and used camelCase names for props.
本文标签: vuejs3How to reformat date valuesStack Overflow
版权声明:本文标题:vuejs3 - How to reformat date values? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738626192a2103479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
DataAgent.date_naissance.value = OldDate[0]
manipulates the state. You should move it in a separatewatch
block. – Raeisi Commented Jan 21 at 20:27