在使用prismjs库高亮代码的时候,HTML、JS和CSS都能正常高亮,但是在现实JSON数据的时候就不高亮,经过一番折腾,最后解决办法如下:
没错,就是这句
import "prismjs/components/prism-json"
pre {
span {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
code {
font-size: 16px !important;
}
}
<template>
<pre class="line-numbers"><code :class="`language-${type}`" v-text="code" /></pre>
</template>
<script setup lang="ts">
import { onMounted } from "vue"
import Prism from "prismjs"
import "prismjs/themes/prism-tomorrow.min.css"
import "prismjs/components/prism-json"
const props = withDefaults(defineProps<{
type?: string
code: string
}>(), {
type: "json"
})
watch(() => props.code, () => {
nextTick(() => {
Prism.highlightAll()
})
})
onMounted(() => {
nextTick(() => {
Prism.highlightAll()
})
})
</script>
<style lang="scss">
pre {
span {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
code {
font-size: 16px !important;
}
}
</style>
完结,撒花??ヽ(°▽°)ノ?