Vue中使用highlight.js實(shí)現(xiàn)代碼高亮顯示以及點(diǎn)擊復(fù)制
本文主要介紹了Vue中使用highlight.js實(shí)現(xiàn)代碼高亮顯示以及點(diǎn)擊復(fù)制,具體如下:
效果如下
第一步 安裝highlight.js
yarn add highlight.js
第二步 在main.js中引入
import hl from 'highlight.js' // 導(dǎo)入代碼高亮文件 import 'highlight.js/styles/a11y-dark.css' // 導(dǎo)入代碼高亮樣式 // 自定義一個(gè)代碼高亮指令 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"> 復(fù)制代碼 </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>
效果如圖:點(diǎn)擊顯示代碼
第四步: 使用組件
<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>` } } }
第五步 實(shí)現(xiàn)點(diǎn)擊復(fù)制代碼clipboard.js。
import Vue from 'vue' import Clipboard from 'clipboard' function clipboardSuccess () { Vue.prototype.$message.success({ content: '復(fù)制成功', duration: 1.5 }) } function clipboardError () { Vue.prototype.$message.error({ content: '復(fù)制失敗', 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) }
到此這篇關(guān)于Vue中使用highlight.js實(shí)現(xiàn)代碼高亮顯示以及點(diǎn)擊復(fù)制的文章就介紹到這了,更多相關(guān)Vue highlight.js代碼高亮顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3實(shí)現(xiàn)搜索項(xiàng)超過n行就折疊的思路詳解
我們在做列表查詢的時(shí)候,會(huì)有很多查詢項(xiàng),如何實(shí)現(xiàn)超過n行查詢項(xiàng)的時(shí)候自動(dòng)折疊起來呢?本文給大家分享vue3實(shí)現(xiàn)搜索項(xiàng)超過n行就折疊的思路詳解,感興趣的朋友一起看看吧2022-06-06Vue openLayers實(shí)現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
OpenLayers是一個(gè)用于開發(fā)WebGIS客戶端的JavaScript包,最初基于BSD許可發(fā)行。OpenLayers是一個(gè)開源的項(xiàng)目,其設(shè)計(jì)之意是為互聯(lián)網(wǎng)客戶端提供強(qiáng)大的地圖展示功能,包括地圖數(shù)據(jù)顯示與相關(guān)操作,并具有靈活的擴(kuò)展機(jī)制2022-09-09Vue倒計(jì)時(shí)3秒后返回首頁Demo(推薦)
這篇文章主要介紹了Vue倒計(jì)時(shí)3秒后返回首頁Demo,倒計(jì)時(shí)結(jié)束后要清除計(jì)時(shí)器,防止內(nèi)存泄漏,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11Vue+webpack+Element 兼容問題總結(jié)(小結(jié))
這篇文章主要介紹了Vue+webpack+Element 兼容問題總結(jié)(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08利用angular、react和vue實(shí)現(xiàn)相同的面試題組件
eact 和angular,vue 這三個(gè)框架最近都比較火,下面這篇文章主要給大家介紹了關(guān)于利用angular、react和vue實(shí)現(xiàn)相同的面試題組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-02-02vue-cli3 配置開發(fā)與測試環(huán)境詳解
這篇文章主要介紹了vue-cli3 配置開發(fā)與測試環(huán)境詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明
這篇文章主要介紹了Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue3發(fā)送驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)現(xiàn)(防止連點(diǎn)、封裝復(fù)用)
這篇文章主要介紹了vue3發(fā)送驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)現(xiàn)(防止連點(diǎn)、封裝復(fù)用),實(shí)現(xiàn)思路是點(diǎn)擊發(fā)送驗(yàn)證碼,驗(yàn)證碼倒計(jì)時(shí),校驗(yàn)手機(jī)號是否正常等一系列操作,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01vue-cli中devServer.proxy相關(guān)配置項(xiàng)的使用
這篇文章主要介紹了vue-cli中devServer.proxy相關(guān)配置項(xiàng)的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04