Vue Tip: Avoid Empty Wrapper for Conditions
There are many situations when you need to conditionally display multiple Vue components. To avoid "empty" wrappers you should use not use HTML tags.
Vue tips · 132 tips
Over the last few years, I've collected a list of valuable tips while developing and writing about Vue & Nuxt. Some are clever, some I use almost every day, and some are more advanced — but they're all useful.
There are many situations when you need to conditionally display multiple Vue components. To avoid "empty" wrappers you should use not use HTML tags.
Eager delay spinners are not a good user experience. They can make a snappy user interface feel slower. We want delay spinners to appear only after a perceivable delay.
You can check if a slot is empty, for example, only to render it if it has content.
The v-memo directive is like a computed prop for the template.
Understand the difference between the watch() and watchEffect() Vue hooks.
A 'fallthrough attribute' is an attribute or v-on event listener that is passed to a component, but is not explicitly declared in the receiving component's props or emits. Common examples of this include class, style, and id attributes.
There are cases when it's useful to specify fallback (i.e. default) content for a slot, to be rendered only when no content is provided.
We can debug properties by passing computed() a second options object.
By default, v-model syncs with the state of the Vue instance on every input event. The .lazy modifier syncs after change events.
v-model has some built-in modifiers, but in some cases, you might need to add your custom modifier.
In some cases, we have to deal with third-party Vue components where we cannot put a ref on inner HTML elements to access them in our Vue code.
You can easily check the Vue version at runtime.
Learn some tools to measure the performance of your Vue application.
Vue Router provides a way to use transitions on route components and animate the navigations. But sometimes, you might want to animate a specific (sub-)component before a route is left.
Instead of passing each property individually to a component, you can bind an object to it.
The watch options support a dot-delimited path as key to watch nested values.
It's possible to use Vuex state to determine if a user is allowed to visit certain Vue Router routes.
Vue provides some special CSS pseudo-selectors you can use in your Vue SFC.
It's possible to use optional chaining in Vue templates.
<script setup> can be used alongside normal <script>. A normal <script> may be needed in special cases.
Using async components is a great way to speed up the initial load time of your app.
When using client-side routing, we may want to scroll to top when navigating to a new route, or preserve the scrolling position of history entries just like real page reload does.
The double mustaches interpret the data as plain text, not HTML. To output real HTML, you will need to use the v-html directive.
We can use webpack to register our Vue.js base components globally.