script/no-computed-properties-in-data
Interdire l’usage d’une propriété calculée dans la fonction data du composant.
| Gravité par défaut | Préréglages | Correction automatique | Options |
|---|---|---|---|
error |
essential, happy-path, ecosystem, nuxt, opinionated |
Non | Aucune |
Champ d’application : Scripts JS/TS des SFC Vue ; les exemples montrent la forme concernée de l’Options API ou de script setup.
Mauvais
data() renvoie this.doubled alors que doubled est encore une propriété calculée, lue avant son initialisation.
<script lang="ts">
export default {
data() {
return { value: this.doubled }
},
computed: {
doubled() { return 1 }
}
}
</script>
Bon
data() renvoie une valeur simple, et doubled lit this.value après l’initialisation.
<script lang="ts">
export default {
data() {
return { value: 1 }
},
computed: {
doubled() { return this.value * 2 }
}
}
</script>
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/no-computed-properties-in-data": "error"
}
},
});
Lancez ensuite vp run lint.