DEV Community

Cover image for 10 Tips to Write Cleaner Vue.js Code

10 Tips to Write Cleaner Vue.js Code

Jakub Andrzejewski on December 09, 2024

When transitioning from one project to another (for example when changing jobs) there is always this period of time when I need to adjust to the ne...
Collapse
 
hinogi profile image

I got a bonus. If you use vite, you might experience different parsing behavior especially when using pug as template language.
In that regard, try to avoid typescript generics in templates.

v-if="list.filter((item: Maybe<ListItem>) => item.active).length > 0"
Enter fullscreen mode Exit fullscreen mode

this will works in vue, but not in the vite devserver since the vite-vue-plugin will complain about no end tag for template because it thinks the generic is also a tag.

Collapse
 
eastmann profile image
Maxim Klochkov

What do you mean by:

handle errors using a central utility function

Would share an example, please?

Collapse
 
jacobandrewsky profile image
Jakub Andrzejewski

Hey there, I have answered it here dev.to/jacobandrewsky/comment/2kcgd :)

Collapse
 
eastmann profile image
Maxim Klochkov • Edited

Hi Jakub!

Thank you for your explanation! I got the idea and I tried handling errors with try/catch in the composable:

the composable handles the error returning to you the result that you can display to a user in a friendly way. And you don't need to write try/catch in every place to achieve that as it is handled by the composable

But when I use such fetch composable in a Vue component and if there is a network or API error I get unhandled promise error message in the console anyway if it is not wrapped in try/catch:

<script setup lang="ts">
import { onMounted } from "vue"
import { useFetch } from "../composables/useFetch.js"

const { data, loading, error, fetchData } = useFetch()

onMounted(async () => {
  await fetchData("https://dummyjson.com/users")
  // unhandled promise error message if something wrong...
})
</script>
Enter fullscreen mode Exit fullscreen mode

How should this console error be handled?

Thread Thread
 
jacobandrewsky profile image
Jakub Andrzejewski

Could you share your code of useFetch.js composable? Maybe some configuration is missing from there :)

Thread Thread
 
jswhisperer profile image
Greg, The JavaScript Whisperer

await fetchData("dummyjson.com/users").catch(e => console.log(e))

Collapse
 
fabio_cardoso_52397447e3e profile image
Fabio Cardoso

Another tip is always you need use a Lot of if and else in your code maybe you should create custom directives in vue. Like some example

From <Button v-if="user.isAdmin" /> to <Button is-admin />

Collapse
 
ciphernutz profile image
Ciphernutz

Awesome tips! These will definitely help in writing cleaner and more efficient Vue.js code.

Collapse
 
emercab profile image
Emerson Cabrera

I loved this post, thank you. Very useful tips.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
jacobandrewsky profile image
Jakub Andrzejewski

When using VueUse axios for example, you get a centralized approach to handle errors (the error property returned by the composable. This means that the composable handles the error returning to you the result that you can display to a user in a friendly way. And you don't need to write try/catch in every place to achieve that as it is handled by the composable.

If not using utility like the one above, you could create your own useApiClient composable that could be a wrapper around the fetch method that will also by default handle errors and populate a property to show you what the error code and message was.

Collapse
 
jswhisperer profile image
Greg, The JavaScript Whisperer
  1. Avoid Overusing Vuex or Pinia Do you have some examples of this suggestion?
Collapse
 
jacobandrewsky profile image
Jakub Andrzejewski

Maybe something like creating a store for a domain that is mapped as a single boolean value? I have seen this approach few times in other projects where store was used as a default for handling state in the application and it resulted in basically writing 50 lines of code (for a store with state, mutations, getters) instead of a plain vue ref.