dev02
dev02

Reputation: 1856

VueJS : Getting innerHTML of custom component

I am trying to build very simple bootstrap alert component but Iam unable to figure out how to get innerHTML of my custom component.

Code:

Vue.component('my-alert', {
  template: '#vue-alert',
  props: ['type', 'title', 'message'],
  created: function() {        
    this.message = this.innerHTML; // <---- NOT WORKING
  }
});

var vm = new Vue({
  el: 'body'
});

HTML:

<my-alert type="success" title="Important">My Message here.</my-alert>

I want to show the component whatever text is provided in it, in this case it should show My Message here.

Here is example of it

Upvotes: 6

Views: 6133

Answers (1)

dev02
dev02

Reputation: 1856

Okay I figured out, I needed to use the <slot></slot>.

<template id="vue-alert">
  <div v-if="show" class="alert alert-{{type}} alert-dismissible" role="alert">
  <button @click="removeAlert" type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span>
  </button>
  <strong>{{title}}!</strong> <slot></slot>
  </div>
</template>

Upvotes: 9

Related Questions