Exclusive tips every week

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.

    Picture of Michael Thiessen

    ๐Ÿ‘‹Hey friend! I work hard to send you amazing stuff each week.

    โ€”ย Michael

    I really love and enjoy reading these emails.

    You are one of the most pro VueJS devs I know, and I am happy that you share this knowledge.

    Fabian Beer

    Here's my latest newsletter

    ๐Ÿ”ฅ (244) Static and dynamic classes, performance tracing, and more

    Hey there!

    Black Friday is around the corner, and I will be offering a massive 35% discount on all of the courses I sell on michaelnthiessen.com.

    Just use the code BLACKFRIDAY25 at checkout to get the discount.

    The discount will be available from November 24 to November 28.

    Here's a quick overview of what you can pick up:

    Of course, you can also just enjoy the newsletter!

    โ€” Michael

    ๐Ÿ”ฅ Static and dynamic classes

    We can add static and dynamic classes to an element at the same time:

    <ul>
    <li
    v-for="item in list"
    :key="item.id"
    class="always-here"
    :class="{ selected: item.selected }"
    >
    {{ item.name }}
    </li>
    </ul>

    This lets you apply basic styling through static classes and then dynamically add other styles as you need them.

    You can also achieve the same thing when using an Object or Array with dynamic classes:

    <ul>
    <li
    v-for="item in list"
    :key="item.id"
    :class="{
    'always-here': true,
    selected: item.selected,
    }"
    >
    {{ item.name }}
    </li>
    </ul>

    Or with an Array:

    <ul>
    <li
    v-for="item in list"
    :key="item.id"
    :class="[
    'always-here',
    item.selected && 'selected',
    ]"
    >
    {{ item.name }}
    </li>
    </ul>

    I prefer splitting them out into class and :class bindings though, since it makes the code clearer. It also makes it less likely to be broken when refactored!

    ๐Ÿ”ฅ Performance Tracing

    Vue allows you to do performance tracing to help you debug any performance issues:

    const app = createApp({});
    app.config.performance = true;

    Once you do this, you can use the official Vue Devtools to debug your app's performance.

    ๐Ÿ”ฅ Calling a Method from Outside of the Component

    You can call a method from outside of a component by giving it a ref:

    <!-- Parent.vue -->
    <template>
    <ChildComponent ref="child" />
    </template>
    <script setup>
    const child = ref(null);
    // Somewhere in the Parent component
    child.value.method();
    </script>

    If you're using the Options API, your syntax is only slightly different:

    <!-- Parent.vue -->
    <template>
    <ChildComponent ref="child" />
    </template>
    <script>
    export default {
    methods: {
    myMethod() {
    // This can be anywhere in the Parent component
    this.$refs.child.method();
    }
    }
    }
    </script>

    Let me explain this one a bit more.

    Sometimes "best practices" don't work for what you're doing, and you need an escape hatch like this.

    Typically, we communicate between components using props and events. Props are sent down into child components, and events are emitted back up to parent components.

    <template>
    <ChildComponent
    :tell-me-what-to-do="someInstructions"
    @something-happened="hereIWillHelpYouWithThat"
    />
    </template>

    Occasionally, you may need your parent to trigger a method in the child component. This is where only passing props down doesn't work as well.

    You could pass a boolean down and have the child component watch it:

    <!-- Parent.vue -->
    <template>
    <ChildComponent :trigger="shouldCallMethod" />
    </template>
    <script setup>
    // Child.vue
    import { watch } from 'vue';
    const props = defineProps({
    trigger: {
    type: Boolean,
    required: true
    }
    });
    watch(props.trigger, (newVal) => {
    if (newVal) {
    // Call the method when the trigger is set to `true`
    someMethodInChild();
    }
    });
    </script>

    This works fine, but only on the first call. If you needed to trigger this multiple times, you'd have to clean up and reset the state. The logic would then look like this:

    1. The Parent component passes true to trigger prop
    2. Watch is triggered, and the Child component calls the method
    3. The Child component emits an event to tell the Parent component that the method has been triggered successfully
    4. The Parent component resets trigger back to false, so we can do this all over again

    Ugh.

    Instead, if we set a ref on the child component we can call that method directly:

    <!-- Parent.vue -->
    <template>
    <ChildComponent ref="child" />
    </template>
    <script setup>
    const child = ref(null);
    // Somewhere in the Parent component
    child.value.method();
    </script>

    Yes, we're breaking the "props down, events up" rule and breaking encapsulation, but it's so much cleaner and easier to understand that it's worth it!

    Sometimes the "best" solution ends up being the worst solution.

    ๐Ÿ“œ The Vite Ecosystem

    Vite has taken web development tooling to a new level.

    This article explores all of the different tools Vite uses and interacts with, and shows just how much it affects the web development community.

    It's very cool to see a project that started out in Vue-land gain wide adoption like this!

    Check it out here: The Vite Ecosystem

    ๐Ÿ“œ Make Your Components Easier to Think About

    I hate thinking.

    Well, actually, I love thinking, but only when Iโ€™m able to solve problems or make progress with it.

    But often our code gets in the way of this. And as one workshop attendee said about reading code, โ€œif youโ€™re confused, itโ€™s not your fault.โ€

    This article goes over some ways you can make your code easier to think about, so youโ€™re less confused and can actually get stuff done.

    Check it out here: Make Your Components Easier to Think About

    ๐Ÿ’ฌ The Code You Don't Write

    "The code you write makes you a programmer. The code you delete makes you a good one. The code you don't have to write makes you a great one." โ€”ย Mario Fusco

    ๐Ÿง  Spaced-repetition: Dynamic Slot Names

    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.

    We can dynamically generate slots at runtime, giving us even more flexibility in how we write our components:

    <!-- Child.vue -->
    <template>
    <div v-for="step in steps" :key="step.id">
    <slot :name="step.name" />
    </div>
    </template>

    Each of these slots works like any other named slot. This is how we would provide content to them:

    <!-- Parent.vue -->
    <template>
    <Child :steps="steps">
    <!-- Use a v-for on the template to provide content
    to every single slot -->
    <template v-for="step in steps" v-slot:[step.name]>
    <!-- Slot content in here -->
    </template>
    </Child>
    </template>

    We pass all of our steps to the Child component so it can generate the slots. Then we use a dynamic directive argument v-slot:[step.name] inside a v-for to provide all of the slot content.

    When might you need something like this?

    I can imagine one use case for a complex form generated dynamically. Or a wizard with multiple steps, where each step is a unique component.

    I'm sure there are more!

    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:

    Here's what others are saying

    I'm starting to think that your newsletter is one of the best things that happened to me this year. I just love these emails.
    Stanislaw Gregor
    I'm somewhere in the upper-middle level at Vue, and I never miss an email you and always find something cool when reading!
    Eduard Climov
    This is the first time where I'm actually enjoying email newsletters. I like your format a lot.
    Fahmi Alfituri
    You have great content in your emails. I seriously learn something from every one of them.
    Titus Decali
    Just wanted to say I enjoy these emails. They encourage me to constantly improve my craft. Fantastic work.
    Joe Radman
    Thanks for another beautiful tip ๐Ÿ™
    Victor Martins Onuoha
    Loving these, and the spaced repetition.
    Mark Goldstein
    I really enjoy reading your emails, because I love Vue. Thanks for these emails.
    Arturo Espinoza
    I really love and enjoy reading these emails. You are one of the most pro VueJS devs I know, and I am happy that you share this knowledge.
    Fabian Beer
    THANK YOU! I did not know about the deep property, so I assumed you simply couldn't watch objects.
    Darryl Noakes
    I really must say you are doing a really great job. Now I am aware of a cleaner and more performant way to use Tailwind. Thanks a lot!
    Henry Eze
    Thank you so much, I really enjoy and appreciate your emails.
    Carlos Gonzalez
    Thanks for sharing great Vue tips.
    Fernando Navarro
    I really enjoy these tips.
    Martin H
    Thank you so much for the weekly Vue education. Thanks and live on longer to educate us more.
    Kabolobari Benakole
    I look forward to your emails every week. This week was something I knew, but I like to be reminded of. Thanks for keeping it up!
    Nathan Strutz
    Thank you for your weekly emails. I always look forward to learning a new tip about Vue or at least relearning old tips :)
    Colin Johnson
    I have really been enjoying your weekly emails, and I also got my two team members to join in on the fun as well.
    Keith Dawson
    Thank you for your newsletter, your explanations have very concise examples and I like it.
    Nicolas Decayeux
    Thanks A LOT for your great work!!! One of the few newsletters that I let pass!
    Martin Tillmann

    Want to level up your Vue skills?

    With over two million reads and 11,067 subscribers, you've come to the right place.

    Subscribe now to get exclusive insights and tips every week.