Home > Blockchain >  Render content between component tags in specific div of component
Render content between component tags in specific div of component

Time:01-11

I have the following vue component:

<template>
  <div >
    <div >
      <div >
        <span  @click="hideMessage"></span>
        <h2>{{ title }}</h2>
        <div v-html="content"></div>
      </div>
      <div >
        <a :href="`tel:${phoneNumber}`" >Call Customer Services</a>
      </div>
    </div>
  </div>
</template>

<script>
  import SiteConstants from '../../Constants/site-constants.js';
  import './styles.scss';

  export default {
    name: 'MessageOverlay',
    props: {
      title: {
        default: '',
        type: String,
      },
      phoneNumber: {
        default: '',
        type: String,
      },
    },
    methods: {
      hideMessage() {
        document.body.classList.remove(SiteConstants.Classes.MessageOpen, SiteConstants.Classes.OverlayOpenWithPhoneLink);
      },
    }
  };
</script>

Which I can use by registering and doing the following:

<message-overlay :title="'Notice!'" :phone-number="0123456789">
  <p>To place orders, an agreement needs to be in place for new projects. </p>
  <p>Please call our dedicated customer service team who will be happy to discuss and set this agreement up for future orders.</p>
</message-overlay>

How do I get the 2 paragraph tags (or any content between the message-overlay tags) in the child component to render in the div with v-html="content" of the parent component template?

CodePudding user response:

You need to add a slot to your message-overlay component

<template>
  <div >
    <div >
      <div >
        <span  @click="hideMessage"></span>
        <h2>{{ title }}</h2>
        <div v-html="content">
          <slot></slot>
        </div>
      </div>
      <div >
        <a :href="`tel:${phoneNumber}`" >Call Customer Services</a>
      </div>
    </div>
  </div>
</template>

https://vuejs.org/v2/guide/components-slots.html

  •  Tags:  
  • Related