Vuejs Dynamic Slots
You’re browsing the documentation for v2.x and earlier. For v3.x, click here.
This page assumes you’ve already read the Components Basics. Read that first if you are new to components.
// Indicates a component with dynamic slots (e.g. Slot that references a v-for // iterated value, or dynamic slot names). // Components with this flag are always force updated. DYNAMICSLOTS = 1. To pass props dynamically, you can add the v-bind directive to your dynamic component and pass an object containing your prop names and values: So your dynamic component would look like this: And in your Vue instance, currentProperties can change based on the current.
- Let’s now learn how to dynamically bind and change classes in HTML elements using VueJS. Problem: You want to dynamically change the class name of an HTML element depending on VueJS data property. Solution: There are two ways you can achieve this, let’s review both of them. #1 Using Object Syntax Let’s work with a simple HTML element that changes its color depending upon the data.
- How to declare a list of slots with dynamic names and dynamic components in a VueJS template? Ask Question Asked 1 year, 4 months ago.
keep-alive
with Dynamic Components
Earlier, we used the is
attribute to switch between components in a tabbed interface:
When switching between these components though, you’ll sometimes want to maintain their state or avoid re-rendering for performance reasons. For example, when expanding our tabbed interface a little:
You’ll notice that if you select a post, switch to the Archive tab, then switch back to Posts, it’s no longer showing the post you selected. That’s because each time you switch to a new tab, Vue creates a new instance of the currentTabComponent
.
Recreating dynamic components is normally useful behavior, but in this case, we’d really like those tab component instances to be cached once they’re created for the first time. To solve this problem, we can wrap our dynamic component with a <keep-alive>
element:
Check out the result below:
Now the Posts tab maintains its state (the selected post) even when it’s not rendered. See this example for the complete code.
Note that <keep-alive>
requires the components being switched between to all have names, either using the name
option on a component, or through local/global registration.
Check out more details on <keep-alive>
in the API reference.
Async Components
In large applications, we may need to divide the app into smaller chunks and only load a component from the server when it’s needed. To make that easier, Vue allows you to define your component as a factory function that asynchronously resolves your component definition. Vue will only trigger the factory function when the component needs to be rendered and will cache the result for future re-renders. For example:
As you can see, the factory function receives a resolve
callback, which should be called when you have retrieved your component definition from the server. You can also call reject(reason)
to indicate the load has failed. The setTimeout
here is for demonstration; how to retrieve the component is up to you. One recommended approach is to use async components together with Webpack’s code-splitting feature:
You can also return a Promise
in the factory function, so with Webpack 2 and ES2015 syntax you can make use of dynamic imports:
When using local registration, you can also directly provide a function that returns a Promise
:
If you’re a Browserify user that would like to use async components, its creator has unfortunately made it clear that async loading “is not something that Browserify will ever support.” Officially, at least. The Browserify community has found some workarounds, which may be helpful for existing and complex applications. For all other scenarios, we recommend using Webpack for built-in, first-class async support.
Handling Loading State
New in 2.3.0+
The async component factory can also return an object of the following format:
Note that you must use Vue Router 2.4.0+ if you wish to use the above syntax for route components.
Let’s now learn how to dynamically bind and change classes in HTML elements using VueJS.
Problem: You want to dynamically change the class name of an HTML element depending on VueJS data property.
Solution:
There are two ways you can achieve this, let’s review both of them.
#1 Using Object Syntax
Let’s work with a simple HTML element that changes its color depending upon the data property value of the VueJS Instance.
We initialize a new VueJS instance with a single data property.
Our goal is to attach a class active
to the h3
element if the value of data property isActive
is true
.
Here is the CSS for active class, add this inside head element of your HTML page.
And here is how we bind the value of data property to the element using object syntax.
Notice that we have used a new directive named v-bind
before the class attribute, which basically evaluates the value in of the data property in the object and if it evaluates to be true it assigns the value to the class attribute. In this case, if isActive is true active class will be applied to the class attribute.
Try changing the value of isActive to false from the console or using the VueJS Dev Tools, you should see the color of text change from green to black (default color without the class).
#2 Using Array Syntax
You can use Array Syntax to dynamically apply the class and also you can use it to toggle the classes.
Vuejs Dynamic Slots App
Let’s add another class named notActive
in our CSS Rules.
Now, in this scenario we want to display the text as green is the data property isActive is true, otherwise, we will show the text in red.
Vuejs Dynamic Slots Game
Here is how you can use the array syntax to dynamically bind and toggle the class.
Try changing the value of property isActive
from developer console or using the VueJS Dev Tools, and see text toggling between green and red and it dynamically switches the class that is attached to it.
Practice Excercises
- Using dynamic binding to disable the button on clicking it. (CodePen Solution Link)
- Place three buttons red , green and blue in a div and change the background color of the div as per the button clicked . (CodePen Solution Link)