🔥 (232) Using useRequestHeader, Reusing Code, and Global Properties

Read this on my blog

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!

Get Advanced Reactivity here

Otherwise, I hope you enjoy the tips and articles I've got for you in this newsletter.

Have a great week,

— Michael

🔥 Using useRequestHeader

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.

See the docs for more info.

🔥 Reusing Code and Knowledge

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:

  • Don't Repeat Yourself (DRY) — Use components and composables to create reusable views and logic. Doing this well is an entire topic all on it's own, which is why I created a whole course on it.
  • Optimize for Change — Most of our time is spent modifying existing code, so it pays to make it easy. If code is new or likely to change, don't worry about abstracting it into a new component yet — duplication is welcome here.
  • Syntax vs. Knowledge — When removing duplication or "drying up" your code, make sure you're encapsulating knowledge and not syntax. Just because code looks the same doesn't mean it is the same.

🔥 Global Properties

It's possible to add global properties to your Vue app in both Vue 2 and Vue 3:

// Vue 3  const app = createApp({});  app.config.globalProperties.$myGlobal = 'globalpropertiesftw';    // Vue 2  Vue.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.js  export default () => ({    $myGlobal: 'globalpropertiesftw',  });

📜 Adding Drag and Drop (video)

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)

📜 Configuration in Nuxt: runtimeConfig vs. appConfig

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

💬 Gall's Law

"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

🧠 Spaced-repetition: Default Content with Nested Slots

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>

🔗 Want more Vue and Nuxt links?

Michael Hoffman curates a fantastic weekly newsletter with the best Vue and Nuxt links.

Sign up for it here.

p.s. I also have a bunch of products/courses:

Unsubscribe

评论

此博客中的热门博文

丁薛祥在“77国集团和中国”气候变化领导人峰会上的致辞(全文)

The magic of scoped slots in Vue ✨ (3/4)