StyleTag.vue 891 Bytes
Newer Older
Anton's avatar
Anton committed
1 2 3 4 5 6 7
<template lang="pug">
  span
</template>

<script>
  export default {
    props: {
8 9
      styles: Array,
      url: String
Anton's avatar
Anton committed
10 11 12 13 14 15 16 17 18 19 20 21 22
    },
    data () {
      return {
        style: undefined
      }
    },
    created () {
      const styles = this.styles || this.$slots.default
      if (Array.isArray(styles)) {
        this.style = styles.reduce((style, val) => style + (val.text || val), '')
      }
    },
    mounted: function () {
23 24 25 26 27 28 29 30 31 32 33 34
      let style
      if (this.url) {
        style = document.createElement('link')
        style.setAttribute('rel', 'stylesheet')
        style.setAttribute('type', 'text/css')
        style.setAttribute('href', this.url)
      }
      else {
        style = document.createElement('style')
        style.setAttribute('type', 'text/css')
        style.append(document.createTextNode(this.style))
      }
Anton's avatar
Anton committed
35 36 37 38
      this.$el.replaceWith(style)
    }
  }
</script>