Vue.js in 2023 – Some tips you should know.
Tags: vue, beginners, webdev, tutorial
Published: 2023-06-21 20:18:03

In next, I'll suggest many tricks to improve your experience with using Vue.js. 

## Vite + Unplugin

It's common to find developers who complain about the “verbosity” of the framework. However, when using Vite with a few more addendums, it is possible to shorten the syntax a lot, leaving the project much cleaner than what is normally found.

Below, a comparison between a common use with Vue 2 x an example using Vue 3+, [Vite](https://vitejs.dev/), [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import) and [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components):

Common:

```vue
<template>
  <aside>
   <ComponentOne />
   <ComponentTwo />
  </aside>
  <main>
    <h1>{{ upper }}</h1>
    <ComponentThree />
    <ComponentFourth />
  </main>
</template>

<script>
import ComponentOne from './components/ComponentOne.vue'
import ComponentTwo from './components/ComponentTwo.vue'
import ComponentThree from './components/ComponentThree.vue'
import ComponentFourth from './components/ComponentFourth.vue'

export default {
  components: {
    ComponentOne,
    ComponentTwo,
    ComponentThree,
    ComponentFourth
  },
  props: {
     title: String
  },
  computed: {
    upper: function () {
      return this.title.toUpperCase()
    }
  }
} 
</script>
```


Reimagined:

```vue
<template>
  <aside>
   <ComponentOne />
   <ComponentTwo />
  </aside>
  <main>
    <h1>{{ upper }}</h1>
    <ComponentThree />
    <ComponentFourth />
  </main>
</template>

<script setup lang="ts">
const props = defineProps<{
  title: string
}>()

const upper = computed(() => props.title.toUpperCase())
</script>
```

> Using unplugin-auto-import, keep attention in file names for don't override other .vue files. I recommend that you choose a structure that guarantees naming that will not cause conflicts.

## Hooks

Decoupling methods using mixins has always been torture. However, with Vue 3 it is possible to use the API separately and reuse methods with ease:

```ts
export const useHook = () => {
  const data = ref('foo')

  const fn = () => {
    // ...
  }

  return { data, fn }
}

// in .vue file

<script setup>
const hook = useHook()

console.log(hook.data.value)
</script>
```

## teleport

[<Teleport>](https://vuejs.org/guide/built-ins/teleport.html) is a built-in component that allows us to “teleport” a part of a component's template into a DOM node that exists outside the DOM hierarchy of that component.

It's a very useful feature to reposition elements that are global or that need to be in another part of the tree at some point. A generic usage is to send an element to the root and position it absolutely:

```vue
<Teleport to="body">
  <UserInfoModal v-if="open" />
</Teleport>
```

> Using a state manager ([Vuex](https://vuex.vuejs.org/), [Pinia](https://pinia.vuejs.org/)) in conjunction with teleport enables greater control and reability of elements.

## defineModel

Since [Vue 3.3.x](https://blog.vuejs.org/posts/vue-3-3), it is possible to create material components and similar features.

Before, the common case is propping with `modelValue` and emitting the changed value to the top component, “simulating” a reactive prop:

Common:

```vue
<template>
  <input v-model="cmp" />
</template>

<script setup lang="ts">
  import { ref, computed } from 'vue'

  const props = defineProps<{
    modelValue: string
  }>()

  const emit = defineEmits(['update:modelValue'])

  const cmp = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emit('update:modelValue', val)
    },
  })
</script>
```

With `defineModel`:

```vue
<template>
  <input v-model="modelValue" />
</template>

<script setup lang="ts">
const modelValue = defineModel()
</script>
```

> Simple, isn't it?

## Nuxt 3

[Nuxt](https://nuxt.com/) it's a Vue framework that you probably know or have heard of. It is currently in a great phase, with a solid ecosystem due to the [modules](https://nuxt.com/modules) and with several features, that will surely add to your project.

## VueUse

[VueUse](https://vueuse.org/) is a collection of essential vue composition utilities. It's a great choice if you're looking for ready-made solutions to common problems, and it can save you a good few hours of development.

## Considerations

Vue.js is at it's your best when it comes to productivity and used concisely, it has everything to add to your work (and please don't rewrite an entire system just because of these tips). Thanks for reading!