If you like Anthony's newsletter on Vue.js Developers, you'll enjoy this one, too.
Join 13,567+ other Vue devs and get exclusive tips and insights delivered straight to your inbox, every week.
đź‘‹Hey friend! I work hard to send you amazing stuff each week.
— Michael
Hey!
Tomorrow is the last day of the Advanced Reactivity course launch and the 35% off discount. If you were hoping to pick it up, don't forget to check it out!
Otherwise, I hope you enjoy the tips and articles I've got for you in this newsletter.
Have a great week,
— Michael
Grabbing a single header from the request couldn’t be easier in Nuxt:
const contentType = useRequestHeader('content-type');
This is super handy in middleware and server routes for checking authentication or any number of things.
If you’re in the browser though, it will return undefined
.
This is an abstraction of useRequestHeaders
, since there are a lot of times where you need just one header.
Don’t Repeat Yourself — an acronym that many know but many don’t correctly understand.
DRY isn’t actually about code, it’s about the knowledge and decisions that are contained in the code. Too often we are just pattern matching on syntax, and that leads us to bad abstractions that should never exist.
Here are some ways we can fix that:
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',});
In this video from LearnVue, we see how easy it can be to add a powerful drag and drop system in to your app.
Check it out here: Adding Drag and Drop (video)
Nuxt provides powerful configuration options, allowing you to adapt your application to different use cases.
The two key parts of Nuxt's configuration system are runtimeConfig and appConfig.
This article will explain the purpose and differences between these two options and show you how to use them.
Check it out here: Configuration in Nuxt: runtimeConfig vs. appConfig
"A complex system that works is invariably found to have evolved from a simple system that worked. The inverse proposition also appears to be true: A complex system designed from scratch never works and cannot be made to work. You have to start over, beginning with a working simple system." — John Gall
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.
If you have multiple levels of nested slots, it's possible to have defaults at each level:
<!-- Parent.vue --><template><Child><slot>We're in the Parent!</slot></Child></template>
<!-- Child.vue --><template><div><slot>We're in the Child!</slot></div></template>
The slot content provided at the highest point in the hierarchy will override everything below it.
If we render Parent
, it will always display We're in the Parent
. But if we render just the Child
component, we get We're in the Child!
.
And if the component rendering the Parent
component provides slot content, that will take precedence over everything:
<!-- Grandparent.vue --><template><Parent>Haha this content rules them all!</Parent></template>
Michael Hoffman curates a fantastic weekly newsletter with the best Vue and Nuxt links.
p.s. I also have a bunch of products/courses: