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)文章
vue自定義橫向滾動(dòng)條css導(dǎo)航兩行排列布局實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue自定義橫向滾動(dòng)條css導(dǎo)航兩行排列布局實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
詳解vuelidate 對于vueJs2.0的驗(yàn)證解決方案
本篇文章主要介紹了vuelidate 對于vueJs2.0的驗(yàn)證解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
vue項(xiàng)目中vue.config.js文件詳解
vue.config.js?是一個(gè)可選的配置文件,如果項(xiàng)目的?(和?package.json?同級(jí)的)?根目錄中存在這個(gè)文件,那么它會(huì)被?@vue/cli-service?自動(dòng)加載,這篇文章主要介紹了vue項(xiàng)目中vue.config.js文件的介紹,需要的朋友可以參考下2024-02-02
Antd的Table組件嵌套Table以及選擇框聯(lián)動(dòng)操作
這篇文章主要介紹了Antd的Table組件嵌套Table以及選擇框聯(lián)動(dòng)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue控制滾動(dòng)條滑到某個(gè)位置的方法實(shí)例
當(dāng)容器有滾動(dòng)條時(shí),有時(shí)需要將滾動(dòng)條滑到某個(gè)位置,下面這篇文章主要給大家介紹了關(guān)于vue控制滾動(dòng)條滑到某個(gè)位置的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
vue 自動(dòng)生成swagger接口請求文件的方法
這篇文章主要介紹了vue 自動(dòng)生成swagger接口請求文件的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01

