Vuejs Dynamic Components

Posted on  by admin

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. 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 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 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 in the API reference.

  • 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.

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:.

It’s a win-win. Development is easier AND your users benefit.

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.

keep-alive with Dynamic Components

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.

Caught a mistake or want to contribute to the documentation?Edit this on GitHub!Deployed onNetlify .

Configure App.vue

Another really cool feature of VueJS is Dynamic Components. What are dynamic components you ask? Well, let’s say you have a main component and you would like to dynamically load other components into the main template by clicking a button or some other means.

This is what can be accomplished with dynamic components.

  • In this tutorial, we’ll create some dynamic components in Vue and test them out in a live demo.

  • Let’s get started with dynamic components in VueJS.

Web Components

To get started, we’ll just add three simple components to our application.

Just for brevity, we’ll name them OneComponent.vue, TwoComponent.vue, and ThreeComponent.vue. They will be located in the components folder of the application.

Here is the markup we’ll put in these three different components. They are super basic, but just different enough so that we will be able to easily differentiate between the components when they get rendered.OneComponent.vue.

Selecting Components Dynamically

Now we want to dynamically make use of these various components in our main App.vue file.

There are a few steps to make this work. First, let’s import those components and register them locally like so.App.vue. Now we want to make use of those components, but how do we do so dynamically?

Normally we would now make use of the selector that corresponds to the particular component we would like to render. We are not going to take that route. Instead, we will use a special selector in VueJS named .

But even in the simplest case, VueJS dynamic components can make your code much more maintainable and adaptable.