Appearance
๐ฅ (#63) Announcing: Vue Tips Collection ๐
June 2022
Announcing...
๐ Vue Tips Collection ๐
A collection of 100+ concise and practical tips on Vue, packaged into a stunning book.
It will be available on June 20th!
(that's only 2.5 weeks from now!)
I've been working hard on this one for a long time, and put my heart and soul into it.
...and now we'll return to our regularly scheduled tips.
โ Michael
๐ฅ Structuring Composables
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.
- Component and directives
provideandinjectdefineProps,defineEmits, anddefineExpose(when usingscript setup)refsandreactivevariables- Computed properties
- Immediate watchers
- Watchers
- Lifecycle methods, in their correct order
- Non-reactive state
- Methods
- Async code using
await(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 Vue 3 styleguide.
The await needs to go at the end because most of your logic you'll want to be registered before the setup function returns. Anything after the await will only be executed asynchronously.
๐ฅ Reactive CSS
In Vue 3 we can use reactive values in our <style> block just like we can in the <template> block:
<style scoped>
.button {
color: v-bind(buttonColor);
}
</style>
Behind the scenes, Vue uses CSS computed properties (aka CSS variables) scoped to each component.
The CSS remains static, but we can dynamically update the CSS variables whenever the reactive value changes.
More info can be found in the docs.
๐ Mark Raw Optimization
This article takes a deeper look at VueUse's useManualRefHistory, and how we can optimize our non-reactive state when it gets large.
Read it here: Mark Raw Optimization
๐ฌ It always takes longer
"It always takes longer than you expect, even when you take into account Hofstadter's Law." โHofstadter's Law
๐ง Spaced-repetition: Directives in Render Functions
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.
Vue comes with some methods to help you use custom directives on your VNodes:
<script setup>
import { resolveDirective, withDirectives, h } from "vue";
// Find the already registered directive by name
const focusDirective = resolveDirective("focus");
// Wrap the button with the directive
const render = () =>
withDirectives(
h("button", {}, []),
// An array of directives to apply
[[focusDirective]]
);
</script>
You can find more info on using withDirectives on the docs.
p.s. I also have two courses: Reusable Components and Clean Components
ๆฅๆบ
ๅๆ https://michaelnthiessen.com/weekly-063-june-01/
ๆฌไฝๅ้็จ็ฅ่ฏๅ ฑไบซ็ฝฒๅ-็ธๅๆนๅผๅ ฑไบซ 4.0 ๅฝ้ ่ฎธๅฏๅ่ฎฎ่ฟ่ก่ฎธๅฏใ