Vuejs Dynamic Slots

  1. Vuejs Dynamic Slot Content
  2. Vuejs Dynamic Slots Game

This page assumes you've already read the Components Basics. Read that first if you are new to components.

  1. Often in your form’s you want the input fields to be populated by the dynamic data, it can be a list of countries in a drop-down or a recent data that you need to fetch from the database via Ajax. In this practice lesson, we will cover how you can populate different input fields in a form with dynamic data. Here is how the form looks like. The data in the select box, radio buttons,.
  2. Dynamic & Async Components. This page assumes you’ve already read the Components Basics. Read that first if you are new to components. Keep-alive with Dynamic Components. Earlier, we used the is attribute to switch between components in a tabbed interface.
Vuejs dynamic slots games

# Dynamic Components with keep-alive

Scoped Slot warns when used inside of dynamic component on regular element #10152 Open MyBeta opened this issue Jun 13, 2019 4 comments May be fixed by #10167.

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:

See the Pen Dynamic components: without keep-alive by Vue (@Vue) on CodePen.

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.

SlotsDynamic

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:

See the Pen Dynamic components: with keep-alive by Vue (@Vue) on CodePen.

Now the Posts tab maintains its state (the selected post) even when it's not rendered.

Vuejs Dynamic Slot Content

Check out more details on <keep-alive> in the API reference.

# Async Components

Vuejs Dynamic Slots Game

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 possible, Vue has a defineAsyncComponent method:

As you can see, this method accepts a factory function returning a Promise. Promise's resolve callback 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.

You can also return a Promise in the factory function, so with Webpack 2 or later and ES2015 syntax you can do:

You can also use defineAsyncComponent when registering a component locally:

# Using with Suspense

Vuejs

Async components are suspensible by default. This means if it has a <Suspense> in the parent chain, it will be treated as an async dependency of that <Suspense>. In this case, the loading state will be controlled by the <Suspense>, and the component's own loading, error, delay and timeout options will be ignored.

The async component can opt-out of Suspense control and let the component always control its own loading state by specifying suspensible: false in its options.

You can check the list of available options in the API Reference