1

I am turning some of my components into re-usable components. I am running into some issues here that I can't figure out. Coming from a React environment, my thoughts are getting jammed up. Basically, I need to be able to make a prop more versatile than just a Boolean or String, or any primitive value. I need to be able to pass "content" to it that could change from page to page depending on what is used for

For example, I have this stateless component:

<template>
   <div class="cts-split-grid cts-alt-header">
      <div>{{title}}</div>
      <div v-if="rightSide" class="cts-split-grid">
        <span class="uk-text-small">Pod or station is open</span>
        <span class="legend-color"></span>
      </div>
   </div>
</template>

<script>
 export default {
     name: "page-alt-header",
     props: {
         title: {
             type: String
         },
         rightSide: {
             type: Boolean
         }
     },
     data() {
         return {
             value: ""
         };
     }
 };
</script>

That I am using this way

 <AltHeader :title="'POD' + currentPodId" rightSide />

As you can see, in the title I am passing an object currentPodId bounded to the component. That was easy since that object only produces a data value.

I want to remove this(below) from the re-usable component and be able to add it in the component using the AltHeader as a rightSide Prop:

<span class="uk-text-small">Pod or station is open</span>
<span class="legend-color"></span>

The reason why is because this component's right side can be anything from an Icon component to a button, to a small block of HTML, etc.

How can I do this? How can I set up rightSide prop to accept anything I pass to it at the component level depending on how I need to use it?

Thanks

1 Answer 1

2

You should use slots

<template>
   <div class="cts-split-grid cts-alt-header">
      <div>{{title}}</div>
      <div v-if="rightSide" class="cts-split-grid">
       <slot></slot>
      </div>
   </div>
</template>

and add right Side content as follows :

  <AltHeader :title="'POD' + currentPodId" rightSide >
      <!-- side right content here -->
  </AltHeader>
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.