Read this on my blog Lots of news recently in the Nuxt ecosystem! First, Nuxt 4 was released! You can read the official announcement here. Second, last week Vercel acquired NuxtLabs (not the Nuxt framework itself) and hired Daniel Roe (lead of Nuxt team), Sébastien Chopin (creator of Nuxt) and a few others from the Nuxt team. This is also huge news, and we covered it in our latest episode of DejaVue (scroll down a bit to find it). I'm working through my own thoughts on this and will have an article soon. But if you want more context, here are a bunch of links to check out: Also, check out this amazing video showing how Nuxt 4 was released. I've also got the regular tips and such here. Enjoy your week! — Michael 🔥 Make Testing Easy Testing is important to do, but it can be hard to do. In my experience, good architecture lends itself to easy-to-write tests (or at least, easier-to-write). The inverse is also true, that difficult-to-write tests are typically a symptom of poor architecture. Of course, sometimes tests are just hard to write, and there's no way around it. The best thing we can do is borrow a tool from mathematics and science, and transform a difficult problem into an easier but equivalent one: - Humble Components — UI is notoriously hard to test, and always has been. So keep as much in Humble Components, components that only receive props and emit events and nothing else. By making our UI as simple as possible we also make it much easier to test.
- Extract logic to composables — And I mean all of your logic. Components (that aren't Humble) should only contain the bare minimum to connect all the different composables together. Think of them as Controller Components, the "C" in MVC.
- Composables are thin layers of reactivity — The easiest thing in the world to test are pure functions that have no dependencies. If you can make the majority of your codebase simple JS or TS code, you've already won. Composables then become simple wrappers that add a layer of reactivity to this business logic.
🔥 Lightweight State Management We can add computed and methods directly on to a reactive object: const counter = reactive({ count: 0, increment() { this.count += 1; }, decrement() { this.count -= 1; }, });
This works because this is set to the object that the method is accessed through, which happens to be the reactive object. Vue's reactivity system uses Proxies to watch for when a property is accessed and updated. In this case, we have a small overhead from accessing the method as a property on the object, but it doesn't trigger any updates. If we had a whole series of counters we can reuse this over and over: const listOfCounters = []; for (const i = 0; i < 10; i++) { const counter = reactive({ id: i, count: 0, increment() { this.count += 1; }, decrement() { this.count -= 1; }, }) listOfCounters.push(counter); }
In our template we can use the counters individually: <div v-for="counter in listOfCounters" :key="counter.id"> <button @click="counter.decrement()">-</button> <button @click="counter.increment()">+</button> </div>
Instead of making the entire object reactive, we can use ref to make only our state reactive: const counter = { count: ref(0), increment() { this.count.value += 1; }, decrement() { this.count.value -= 1; }, };
This saves us a small and likely unnoticeable overhead. But it also feels somewhat better since we're being more thoughtful with our use of reactivity instead of spraying it everywhere. Here's our example from before, but this time I'm going to add in a factory function to make it more readable: const createCounter = (i) => ({ id: i, count: ref(0), increment() { this.count.value += 1; }, decrement() { this.count.value -= 1; }, }); const listOfCounters = []; for (const i = 0; i < 10; i++) { listOfCounters.push(createCounter(i)); }
Of course, we can use a factory method with the previous reactive method as well. 🔥 Using two script blocks The
<script setup> // Composition API import { ref } from 'vue'; console.log('Setting up new component instance'); const count = ref(0); </script> <script> // ...and the options API too! export default { name: 'DoubleScript', }; </script>
import { provide } from 'vue'; const someMethodInTheParent = () => {}; provide('method', someMethodInTheParent)
Then, inject it into the child component: import { inject } from 'vue'; const method = inject('method'); method();
In Vue 2, you can also use the instance property $parent : // Tight coupling like this is usually a bad idea this.$parent.methodOnParentComponent();
This is simpler, but leads to higher coupling and will more easily break your application if you ever refactor. You can also get direct access to the application root, the very top-most component in the tree, by using $root . Vue 2 also has $children , but these were taken out for Vue 3 (please don't use this one). When would these be useful? There are a few different scenarios I can think of. Usually, when you want to abstract some behaviour and have it work "magically" behind the scenes. You don't want to use props and events to connect up a component in those cases. Instead, you use provide /inject , $parent , or $root , to automatically connect the components and make things happen. (This is similar to the Compound Component pattern) But it's hard to come up with an example where this is the best solution. Using provide /inject is almost always the better choice. 🔗 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: |
评论
发表评论