Hey all!
Last week I didn't send out a newsletter because I was busy with the holidays and was hanging out with family instead.
I'm back now and ready to share some tips with you.
Once I'm back to work I'll be able to polish up the next set of patterns for Composable Design Patterns and send them out (if you've bought the early access).
Hopefully the new year is going well for you so far!
— Michael
To keep your composables — those extracted functions written using the composition API — neat and easy to read, here's a way to organize the code.
provide
and inject
defineProps
, defineEmits
, and defineExpose
(when using script setup
)refs
and reactive
variablesawait
(or Promises if you're into that sort of thing)Why this order? Because it more or less follows the order of execution of the code.
It's also based on the this linting rule.
Did you know that you can destructure in a v-for
?
<liv-for="{ name, id } in users":key="id">{{ name }}</li>
It's more widely known that you can grab the index out of the v-for by using a tuple like this:
<li v-for="(movie, index) in ['Lion King','Frozen','The Princess Bride']">{{ index + 1 }} - {{ movie }}</li>
When using an object you can also grab the key:
<li v-for="(value, key) in {name: 'Lion King',released: 2019,director: 'Jon Favreau',}">{{ key }}: {{ value }}</li>
It's also possible to combine these two methods, grabbing the key as well as the index of the property:
<li v-for="(value, key, index) in {name: 'Lion King',released: 2019,director: 'Jon Favreau',}">#{{ index + 1 }}. {{ key }}: {{ value }}</li>
It's possible to add global properties to your Vue app in both Vue 2 and Vue 3:
// Vue 3const app = createApp({});app.config.globalProperties.$myGlobal = 'globalpropertiesftw';// Vue 2Vue.prototype.$myGlobal = 'globalpropertiesftw';
I would recommend prefixing any global properties with a $
.
This helps prevent naming conflicts with other variables, and it's a standard convention that makes it easy to spot when a value is global.
This global property can be accessed directly off of any component when using the Options API:
computed: {getGlobalProperty() {return this.$myGlobal;},},
Why can't this be used with the composition API?
Because the composition API is designed to be context-free and has no access to this
.
Instead, you can create a simple composable to access your globals:
<script setup>import useGlobals from './useGlobals';const { $myGlobal } = useGlobals();</script>
// useGlobals.jsexport default () => ({$myGlobal: 'globalpropertiesftw',});
This DejaVue episode comes as a little special! As a belated Christmas present, we got everything, literally everything that was discussed on this podcast about one big topic: Composition API vs. Options API.
A lot of our previous guests had their own takes on the two APIs — and some might surprise you! So, why not tuning in and hear fifteen people talk about their opinions, insights and suggestions when it comes to using their preferred API in Vue — and why.
Watch on YouTube or listen on your favorite podcast platform.
Chapters:
In case you missed them:
Vite has taken web development tooling to a new level.
This article explores all of the different tools Vite uses and interacts with, and shows just how much it affects the web development community.
It's very cool to see a project that started out in Vue-land gain wide adoption like this!
Check it out here: The Vite Ecosystem
So far in this series we’ve covered a lot on how to use Prisma in our Nuxt apps.
But we’ve left out a major piece — actually being able to modify the data in the database.
A pretty crucial part of the puzzle I think!
In this article I'll show you how to modify data in your database using Prisma.
Check it out here: Prisma with Nuxt: Modifying Data with Prisma (5 of 5)
Here are some upcoming events you might be interested in. Let me know if I've missed any!
The biggest Vue conference in the world! A two-day event with workshops, speakers from around the world, and socializing.
A great Vue conference, this year held in Tampa. Two days of conference talks, plus a day for workshops.
"You're bound to be unhappy if you optimize everything." — Donald Knuth
The best way to commit something to long-term memory is to periodically review it, gradually increasing the time between reviews 👨‍🔬
Actually remembering these tips is much more useful than just a quick distraction, so here's a tip from a couple weeks ago to jog your memory.
It took me way too long to figure this one out, but here it is:
// Doesn't change when route changesconst route = useRoute();// Changes when route changesconst path = useRoute().path;
If we need the full route object in a reactive way, we can do this:
// Doesn't change when route changesconst route = useRoute();// Changes when route changesconst route = useRouter().currentRoute.value;
With the Options API you can use $route
and $router
to get objects that update whenever the route changes.
Since Nuxt uses Vue Router internally, this works equally well in Nuxt and vanilla Vue apps.
Here's a demo to see this for yourself: Demo
Michael Hoffman curates a fantastic weekly newsletter with the best Vue and Nuxt links.
p.s. I also have a bunch of products/courses: