Do I have to use an event bus to realize the slot based version? Listen on events when using slots. Vue.js - The Progressive JavaScript Framework. Understanding Component Slots with Vue.js Vue.js. By Joshua Bemenderfer. Published on February 5.
Do you find yourself wanting to extend a component's template? Vue provides several means of extracting and reusing the component logic and state, but templates are deemed single-use only.
In this article, I'll present a solution using the HTML template pre-processor Pug. It's a bit of a hack, but it does work!
If you're looking to extend a component's logic and state, you probably want to read my other article Extending Vue.js Components which covers composition functions, mixins, higher-order components, etc. Here we're talking about the template.
Case study: a survey app
Let's say you're trying to make a component-based survey app with Vue which looks like this:
Notice that each survey question has a different input type:
- Text input
- Select input
- Radio input
An obvious architecture would be to make each question into a separate component, which is what we're going to do. Well name these components:
SurveyInputText
SurveyInputSelect
SurveyInputRadio
Base component
Let's first create a 'base' component called SurveyInput.vue. Notice the following about it:
- The
question
prop is going to be common across each component. This captures the question text e.g. 'What is your name'. - We don't have any shared logic, but you could add it e.g. validation and error logic
- I'm leaving a gap where the input element will need to go. This is where we'll extend the template as you'll shortly see.
SurveyInput.vue
State and logic reuse
Ignoring the template for a moment, what will be our strategy for extraction and reuse of the state/logic? What we choose here will not affect our ability to extend the component template, so feel free to use mixins, higher-order components, or even the new composition API. I'm going to keep it simple and use the extends
component option.
Again, there's still a hanging question about how we get the base template into this subcomponent, as the Vue.extends
API doesn't apply to templates!
SurveyInputText.vue
Never miss a new post!
- The
question
prop is going to be common across each component. This captures the question text e.g. 'What is your name'. - We don't have any shared logic, but you could add it e.g. validation and error logic
- I'm leaving a gap where the input element will need to go. This is where we'll extend the template as you'll shortly see.
SurveyInput.vue
State and logic reuse
Ignoring the template for a moment, what will be our strategy for extraction and reuse of the state/logic? What we choose here will not affect our ability to extend the component template, so feel free to use mixins, higher-order components, or even the new composition API. I'm going to keep it simple and use the extends
component option.
Again, there's still a hanging question about how we get the base template into this subcomponent, as the Vue.extends
API doesn't apply to templates!
SurveyInputText.vue
Never miss a new post!
Get our latest post in your inbox every Tuesday by subscribing to the Vue.js Developers Newsletter .
This subscription also includes Vue.js Developers promotional emails. You can opt-out at any time. View our privacy policy .
This form is protected by reCAPTCHA. The Google privacy policy and terms of service apply.
Conventional options for extending the template (and why they aren't ideal)
A reasonable Vue user would first consider the following design patterns for the template:
- Props-driven template logic
- Slots
But as I'll show, both have downsides for the use case we're considering, so there is a legitimate case for wanting to use the template-extension hack I'm going present.
Use Slot Vuejs
Props-driven template logic
Rather than making separate components for each input type, you could create a single mega-component and then specify the template using conditional logic fed by a prop called question-type
i.e.
SurveyInput.vue
Now you can declare your questions in the parent, Survey.vue, like this:
Survey.vue
The downside of this approach is that it doesn't scale well. Once you get, say, 3-4 different question types the template will become big and messy.
Slots
Use Slot Vue Json
Another conventional approach that could work is to put a slot where the input belongs.
SurveyInput.vue
Using this approach the parent can provide the correct markup for each input like this:
Survey.vue
The downside to slots, though, is that you now have to organize the data model differently. Now, the parent owns the state of each input and would have to communicate it with each child component using props/events, adding complex logic and verbosity.
Survey.vue
Hopefully, I've convinced you that template logic and slots aren't ideal, so now let's look at how we can extend a component's template like we can with its state/logic.
To do this, we're going to need an HTML template pre-processor.
Pug HTML pre-processor
By using vue-loader
and the lang
property of the template in a single-file component, we can use HTML template pre-processing tools like Slim, Handlebars, etc.
My hack for extending Vue templates is to use the include
and extends
options provided by Pug (previously Jade).
First, add Pug to your project. Vue Loader should pick this up without any further config needed.
Easy to access by a combination of the Metro and a shuttle bus, Casino de Montreal is the place where those who love games of chance can find a really good time. The first thing that will get your attention when you first see the Casino de Montreal is the design and size of the building. Montreal casino shuttle downtown. Answer 1 of 7: Just wondering if the casino offers a shuttle to the downtown hotels that we can catch a ride on? We are staying at the Loews hotel, just wondering what would be the best way to get there without a vehicle at night. Casino de Montreal: Free Shuttle. See 929 traveler reviews, 196 candid photos, and great deals for Montreal, Canada, at Tripadvisor.
Now, let's convert our base component's template to Pug syntax:
Notice that we use block input
to declare an 'outlet' where the subcomponent content will be distributed.
Important: the outlet is kind of like a slot but the important difference is that template processing occurs at compile-time, not at run-time as it would with slots.
Creating a base template
So here's where it gets slightly messy. If we want our child components to extend the template we first need to put it into its own file SurveyInput.pug
:
SurveyInput.pug
Now we can include
this file in our base component so it can still be used as a normal single-file component:
SurveyInput.vue
It's a shame to have to do that since it kind of defeats the purpose of 'single file' components. Probably someone could make a custom webpack loader to avoid having to do this.
Extending to a subcomponent
To extend the subcomponent's template from the base, you'll need to covert its template to Pug as well.
The subcomponents use the extends
feature of Pug which includes the base component and outputs any custom content in the input
block (again, similar to slots, but it happens at compile-time).
Use Slot Vue Js Online
SurveyInputText.vue
Use Slot Vue Jsp
Here's what the subcomponent's template would effectively look like after extending the base and being translated to a regular HTML Vue template:
Bring it all together
Casino no deposit bonus free money 2020. Using this strategy we can go ahead and create the other two subcomponents SurveyInputSelect
and SurveyInputRadio
. If we then use them in a project our main template might look like this:
Survey.vue
And here's how the rendered markup would look:
Use Slot Vue Js Download
Happy hacking!