Vue中使用highlight.js實現(xiàn)代碼高亮顯示以及點擊復制
本文主要介紹了Vue中使用highlight.js實現(xiàn)代碼高亮顯示以及點擊復制,具體如下:
效果如下
第一步 安裝highlight.js
yarn add highlight.js
第二步 在main.js中引入
import hl from 'highlight.js' // 導入代碼高亮文件 import 'highlight.js/styles/a11y-dark.css' // 導入代碼高亮樣式 // 自定義一個代碼高亮指令 Vue.directive('highlight', function (el) { const blocks = el.querySelectorAll('pre code') blocks.forEach((block) => { hl.highlightBlock(block) }) })
第三步 創(chuàng)建組件
<template> <div class="copy-code-container"> <div class="copy-container flex-row"> <a-tooltip> <template slot="title"> 復制代碼 </template> <div class="ant-btn" @click="handleCopy(code, $event)"> <a-icon type="copy"></a-icon></div> </a-tooltip> <a-tooltip> <template slot="title"> 顯示代碼 </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.1s; &.show-code { height: 100%; } .code-box { ::v-deep .hljs { padding: 0 20px; line-height: 25px; } } } } </style>
效果如圖:點擊顯示代碼
第四步: 使用組件
<copy-code :code="code"> </copy-code> export default { data () { return { code: `<template> <div> <a-button type="primary"> Primary </a-button> <a-button>Default</a-button> <a-button type="dashed"> Dashed </a-button> <a-button type="danger"> Danger </a-button> <a-config-provider :auto-insert-space-in-button="false"> <a-button type="primary"> 按鈕 </a-button> </a-config-provider> <a-button type="primary"> 按鈕 </a-button> <a-button type="link"> Link </a-button> </div> </template>` } } }
第五步 實現(xiàn)點擊復制代碼clipboard.js。
import Vue from 'vue' import Clipboard from 'clipboard' function clipboardSuccess () { Vue.prototype.$message.success({ content: '復制成功', duration: 1.5 }) } function clipboardError () { Vue.prototype.$message.error({ content: '復制失敗', 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) }
到此這篇關于Vue中使用highlight.js實現(xiàn)代碼高亮顯示以及點擊復制的文章就介紹到這了,更多相關Vue highlight.js代碼高亮顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue openLayers實現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
OpenLayers是一個用于開發(fā)WebGIS客戶端的JavaScript包,最初基于BSD許可發(fā)行。OpenLayers是一個開源的項目,其設計之意是為互聯(lián)網(wǎng)客戶端提供強大的地圖展示功能,包括地圖數(shù)據(jù)顯示與相關操作,并具有靈活的擴展機制2022-09-09Vue+webpack+Element 兼容問題總結(小結)
這篇文章主要介紹了Vue+webpack+Element 兼容問題總結(小結),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08利用angular、react和vue實現(xiàn)相同的面試題組件
eact 和angular,vue 這三個框架最近都比較火,下面這篇文章主要給大家介紹了關于利用angular、react和vue實現(xiàn)相同的面試題組件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。2018-02-02vue-cli3 配置開發(fā)與測試環(huán)境詳解
這篇文章主要介紹了vue-cli3 配置開發(fā)與測試環(huán)境詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明
這篇文章主要介紹了Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue3發(fā)送驗證碼倒計時功能的實現(xiàn)(防止連點、封裝復用)
這篇文章主要介紹了vue3發(fā)送驗證碼倒計時功能的實現(xiàn)(防止連點、封裝復用),實現(xiàn)思路是點擊發(fā)送驗證碼,驗證碼倒計時,校驗手機號是否正常等一系列操作,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01vue-cli中devServer.proxy相關配置項的使用
這篇文章主要介紹了vue-cli中devServer.proxy相關配置項的使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04