Results the following

Step 1 Install highlight.js

yarn add highlight.js 
Copy the code

The second step is introduced in main.js

import hl from 'highlight.js' // Import code highlighting file
import 'highlight.js/styles/a11y-dark.css' // Import code highlighting styles

// Define a code highlighting directive
Vue.directive('highlight'.function (el) {
  const blocks = el.querySelectorAll('pre code')
  blocks.forEach((block) = > {
    hl.highlightBlock(block)
  })
})
Copy the code

Step 3 Create the component

<template>
  <div class="copy-code-container">
    <div class="copy-container flex-row">
        <a-tooltip>
          <template slot="title">Copy the code</template>
          <div class="ant-btn" @click="handleCopy(code, $event)"> <a-icon type="copy"></a-icon></div>
        </a-tooltip>

      <a-tooltip>
        <template slot="title">According to the code</template>
        <a-icon @click="handeShowCode" type="code" />
      </a-tooltip>
    </div>

    <div class="code-palce-container" :class="{ 'show-code': showCode }">
      <div class="code-box" v-highlight>
        <pre>
            <code class="javascirpt">{{code}}</code>
        </pre>
      </div>
    </div>
  </div>
</template>

<script>
import clip from '@/utils/clipboard' // use clipboard directly

export default {
  data () {
    return {
      showCode: false}},props: {
    code: {
      type: String.default: ' '}},methods: {
    handeShowCode () {
      this.showCode = !this.showCode
    },
    handleCopy (text, event) {
      clip(text, event)
    }
  }
}
</script>

<style lang="less" scoped>
.copy-code-container {
  width: 100%;

  .copy-container {
    width: 100%;
    height: 50px;
    justify-content: center;
    align-items: center;
    position: relative;

    .ant-btn{
      width: 58px;
      height: 38px;
      margin: 0;
      border: none;
      box-shadow: none;
      background-color: transparent;
      padding: 0;
    }

    i {
      cursor: pointer;
      font-size: 18px;
      padding: 10px 20px; }}.code-palce-container {
    width: 100%;
    height: 0;
    overflow: hidden;
    transition: all linear 0.1 s;

    &.show-code {
      height: 100%;
    }

    .code-box {
      ::v-deep .hljs {
        padding: 0 20px;
        line-height: 25px; }}}}</style>

Copy the code

It looks like this: click to show the code

Step 4: Use components

<copy-code :code="code"> </copy-code>

export default {
  data () {
    return {
      code: `
      `}}}Copy the code

Step 5: Click to copy the code clipboard.js.Clipboard. Js copy address

import Vue from 'vue'
import Clipboard from 'clipboard'

function clipboardSuccess () {
  Vue.prototype.$message.success({
    content: 'Copy successful'.duration: 1.5})}function clipboardError () {
  Vue.prototype.$message.error({
    content: 'Replication failed'.duration: 1.5})}export default function handleClipboard (text, event) {
  const clipboard = new Clipboard(event.target, {
    text: () = > text
  })
  clipboard.on('success'.() = > {
    clipboardSuccess()
    clipboard.destroy()
  })
  clipboard.on('error'.() = > {
    clipboardError()
    clipboard.destroy()
  })
  clipboard.onClick(event)
}

Copy the code

Please correct any mistakes.