Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 461
dev02 Avatar asked Aug 20 '16 15:08

dev02


1 Answers

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>
like image 126
dev02 Avatar answered Oct 30 '22 20:10

dev02