0

Let's start from this

<div class="form-group" :class="{'has-error':determineError('content')}">
  <label>Content Label</label>
  <div class="mandat">*</div>
  <input v-model="form.content" name="content" v-validate="'required|min:5|max:100'" class="form-control">
</div>

The first thing I would like to obtain is to put this piece of code somehow inside a component, something like this:

Vue.component('form-group', {
    ...
    template: `<div class="form-group" :class="{'has-error':determineError('content')}">
      <label>Content Label</label>
      <div class="mandat">*</div>
      <input v-model="form.content" name="content" v-validate="'required|min:5|max:100'" class="form-control">
    </div>`
});

As you can see I still have the input field right there. What I would like to do is pass any piece of code instead and the current component must inherit parent's context.

Something like

<form-group>
  <template>
    <input v-model="form.content" name="content" v-validate="'required|min:5|max:100'" class="form-control">
  </template>
</form-group>

How can this be achieved? Notice that I still use parent's context. If using parent's context is not possible, then how can I achieve this in the simplest way?

1 Answer 1

1

You have to use slots, which are expanded in the component template with the contents passed by the parent.

In the form-group component:

<template>
  <div class="form-group" :class="{'has-error':determineError('content')}">
    <label>Content Label</label>
    <div class="mandat">*</div>
    <slot v-bind:form="form"></slot>
  </div>
</template>

You can also add a fallback content inside the <slot> (a default input maybe). Note we are passing the context for the slot contents (see Scoped Slots).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.