简体   繁体   中英

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM