Return to site

Vue Slot Props Example

broken image


  1. Vue Slot Examples
  2. Vue Props Types
  3. Vue Slot Class
  4. Vue Slot Props Examples
  5. Vue Props Update

Tutorial

While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.

Vue Slot Examples

Oftentimes you will need to allow your parent Vue components to embed arbitrary content inside of child components. (Angular devs, you know this as transclusion or content projection.) Vue provides an easy way to do this via slots.

Basic Usage

Vue.js Slots; Vue.js Component Props; Vue.js Events; Vue.js Components Communication; Vuex, the Vue.js State Manager; Vue, use a component inside another component; Vue, how to use a prop as the class name; How to use SCSS with Vue.js Single File Components; Using Tailwind with Vue.js; The Vue Router; Dynamically show a Vue component; The Vue. In the two examples above, we happen to pass string values, but any type of value can actually be passed to a prop. Passing a Number Even though `42` is static, we need v-bind to tell Vue that -

Vue

To allow a parent component to pass DOM elements into a child component, provide a element inside the child component. You can then populate the elements in that slot with

MyParagraph

.

ParentComponent.vue

Vue Props Types

Note: If there is no element in the child's template, any content from the parent will be silently discarded.

Fallback Content

If the parent does not inject any content into the child's slot, the child will render any elements inside its tag, like so:

ParentComponent.vue

Named Slots

Having one slot to inject content into is nice and all, but oftentimes it would be preferable to be able to inject various types of content at different locations in the child component. Say you're making a burger component. You want the top and bottom buns to be predictably at the top and bottom of the burger, (don't you?) but also want to be able spread things on the bun halves.

Vue allows us to do this by way of named slots. Named slots are simply slots with a name attribute to allow for namespaced content injection.

SecretRecipeBurger.vue

Obviously I don't have a clue as to how to make burgers, but that should at least serve as an understandable guide to Vue slots. Enjoy!

bySai gowtham

In this tutorial, we are going to learn about propsin vue.js apps with the help of examples.

Props

In Vue.js props helps us to pass the data from parent components to its child components.

Registering props

To use the props in Vue components first we need to register the props.

Let's create a new component called Button.

In the above code, we have registered a prop called name inside the props array. The registered props can be used inside the template just like data properties.

Passing the data to the props

Now, we can pass the data to that prop as a custom HTML name attribute.

Passing functions to the props

Let's register a second prop to our Button component.

Here we registered a handleClick prop which is attached to @click event to the button. Now we need to pass the function to that prop.

Inside the template we passed the shareMe function to our :handleClick prop.

Vue Slot Class

for dynamic values we need to use (colon) :propname instead of propname otherwise Vue treated it as JavaScript string.

Props

To allow a parent component to pass DOM elements into a child component, provide a element inside the child component. You can then populate the elements in that slot with

MyParagraph

.

ParentComponent.vue

Vue Props Types

Note: If there is no element in the child's template, any content from the parent will be silently discarded.

Fallback Content

If the parent does not inject any content into the child's slot, the child will render any elements inside its tag, like so:

ParentComponent.vue

Named Slots

Having one slot to inject content into is nice and all, but oftentimes it would be preferable to be able to inject various types of content at different locations in the child component. Say you're making a burger component. You want the top and bottom buns to be predictably at the top and bottom of the burger, (don't you?) but also want to be able spread things on the bun halves.

Vue allows us to do this by way of named slots. Named slots are simply slots with a name attribute to allow for namespaced content injection.

SecretRecipeBurger.vue

Obviously I don't have a clue as to how to make burgers, but that should at least serve as an understandable guide to Vue slots. Enjoy!

bySai gowtham

In this tutorial, we are going to learn about propsin vue.js apps with the help of examples.

Props

In Vue.js props helps us to pass the data from parent components to its child components.

Registering props

To use the props in Vue components first we need to register the props.

Let's create a new component called Button.

In the above code, we have registered a prop called name inside the props array. The registered props can be used inside the template just like data properties.

Passing the data to the props

Now, we can pass the data to that prop as a custom HTML name attribute.

Passing functions to the props

Let's register a second prop to our Button component.

Here we registered a handleClick prop which is attached to @click event to the button. Now we need to pass the function to that prop.

Inside the template we passed the shareMe function to our :handleClick prop.

Vue Slot Class

for dynamic values we need to use (colon) :propname instead of propname otherwise Vue treated it as JavaScript string.

Validating props

Vue Slot Props Examples

So far we are registering props by using an array syntax but there is a second way to register and validate props by using object syntax.

Let's validate our props present in Button component.

Here we added two properties which are required and type of data the prop is accepting.

Now if we fail to pass the data to that prop Vue.js shows us an error inside our browser's console.

Vue Props Update

Available Type checks





broken image