<template>
  <div class="lx-copy">
    <input type="text" class="copy-contents" :value="code" />
    <slot name='code'></slot>
    <div class="lx-copy__btn" @click="copyCode">
      <slot name='btn'></slot>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'lxCopy',
    props: {
      code: {
        type: String,
        default: ''
      },
    },
    data () {
      return {

      }
    },
    mounted() {

    },
    methods: {
      // 点击复制
      copyCode() {
        var elem = document.querySelector('.copy-contents')
        elem.select(); //选择对象 （全选）
        elem.setSelectionRange(0, elem.value.length);
        document.execCommand('copy'); //执行浏览器复制命令
        this.$emit('copy')
      }
    },
  }
</script>
