Hey!
I just wrote a comprehensive guide on redirecting in Nuxt.
It turns out there are a lot of different ways to do this, depending on what you need done!
You can read it on the Mastering Nuxt blog:
How to Redirect in Nuxt (Every Single Way)
In other news, Alex and I are continuing to put out new podcast episodes each week. Interviews with Daniel Roe and Evan You are coming up in the next couple weeks, so stay tuned for that!
The next thing I'm working on is the Nuxt Tips Collection. I'm already at 64 tips and counting, almost all of them completely brand new for the book!
Enjoy your tips, and enjoy the rest of your week!
— Michael
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:
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>
Is it better to use ref
or reactive
when using the composition API?
Here are a few situations where ref
is better than reactive
.
Using ref
on objects makes it clear where an object is reactive and where it's just a plain object:
// I can expect this ref to update reactivelyif (burger.value.lettuce) {// ...}// I have no clue if this value is reactiveif (burger.lettuce) {// ...}
When using one of the watch
methods, refs
are automatically unwrapped, so they're nicer to use:
// Refconst refBurger = ref({ lettuce: true });watch(// Not much, but it's a bit simpler to work withrefBurger,() => console.log("The burger has changed"),{ deep: true });// Reactiveconst reactiveBurger = reactive({ lettuce: true });watch(reactiveBurger,() => console.log("The burger has changed"),);
One last reason why refs
make more sense to me — you can put refs
into a reactive
object. This lets you compose reactive
objects out of refs
and still use the underlying refs
directly:
const lettuce = ref(true);const burger = reactive({// The ref becomes a property of the reactive objectlettuce,});// We can watch the reactive objectwatchEffect(() => console.log(burger.lettuce));// We can also watch the ref directlywatch(lettuce, () => console.log("lettuce has changed"));setTimeout(() => {// Updating the ref directly will trigger both watchers// This will log: `false`, 'lettuce has changed'lettuce.value = false;}, 500);
Today on DejaVue, Alex and Michael are joined by Game and Web Developer Eduard But, who created Geotastic - a Vue-based browser game revolving around geographical knowledge and uses Googles Maps and Streetview API! Starting with Edu's background in programming and web development and initial experiences with Vue.js, we dive deep into how it happened he got into Game Development, which multiplayer game he built first and why and eventually talk about Geotastic.
With more than 1.5 Million registered users, Edu reveals details about the financial model, how he keeps things up and running, challenges while maintaining and which new game mode will come to Geotastic soon! Tune in to hear all of the above and more.
Watch on YouTube or listen on your favourite podcast platform.
Chapters:
In case you missed them:
Design patterns are incredibly useful in writing code that works well over the long run. They let us use proven solutions to problems that basically every single app has.
But there isn’t a lot written about how design patterns apply specifically to Vue.
In this article, I cover 12 design patterns that I think are crucial to writing great Vue apps.
Check it out here: 12 Design Patterns in Vue
One of Vue's core features is the use of props. Props are how we pass data around in Vue, from parent to child components.
But not all props are created equal.
There are three main kinds:
Check it out here: 3 Kinds of Props in Vue
Here are some upcoming events you might be interested in. Let me know if I've missed any!
The first Czech Vue.js conference, taking place in Cinema City - Nový Smíchov
A community-driven Vue conference in Germany. Listen to great talks from great speakers and meet the wonderful VueJS Community.
My favourite Vue conference, in my own backyard! A three-day event with workshops, speakers from around the world, and socializing.
"A good way to stay flexible is to write less code." — Pragmatic Programmer
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.
When building a UI, there are many different states that you need to consider:
200ms
before showing a spinner. If the data loads before that, it feels faster than if you quickly flash the loading spinner on and then off again.Michael Hoffman curates a fantastic weekly newsletter with the best Vue and Nuxt links.
p.s. I also have four products/courses: Clean Components Toolkit, Vue Tips Collection 2, Mastering Nuxt 3, and Reusable Components