What is the best way to structure a component with the new Vue 3 script setup? #6911
-
So now we have the new <script setup>
//
</script> Which is great and I'm loving to use it, but I noticed everything can be placed wherever the developer want, doesn't seems to have a standard, not even some eslint rule to preventing a structure in such files, as we have in Options API. I wonder what's the best way to organize a component file Here's some simple example of what way I'm taking on this type of scripts: <script setup>
import { computed } from 'vue'
import { ref } from 'vue'
const props = defineProps({
type: {
type: String,
default: 'text'
}
})
const type = ref(props.type)
const isPassword = computed(() => {
return props.type === 'password'
})
const passwordIconVisibility = computed(() => {
return type.value === 'password'
? 'visibility_off'
: 'visibility'
})
const togglePasswordVisibility = () => {
type.value = type.value === 'password'
? 'text'
: 'password'
}
</script> |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Yeah, pretty much that's it. The same way we would organize stuff in a class. So in a Vue SFC: Hope I could help. Wishing you health and success. Have fun! |
Beta Was this translation helpful? Give feedback.
-
@banlify would you have the descency explaining why my answer is laughable!? Unless the laughing reaction was a mistake. But if it wasn't a mistake, illuminate us with your wisdom. |
Beta Was this translation helpful? Give feedback.
Yeah, pretty much that's it.
The same way we would organize stuff in a class.
imports first, then fields, then getters/setters,
and then methods.
So in a Vue SFC:
imports
Compiler macros (defineProps, defineEmits, definePage (unplugin-vue-router), definePageMeta (Nuxt 3)
props
pinia stores
computed/watchers
life-cycle hooks
methods
Hope I could help. Wishing you health and success. Have fun!