vue 项目使用 prismjs 高亮显示代码

徐洋
2023-12-01

在框架中写文档所以想要代码高亮显示

1.首先安装prismjs 

npm install prismjs

2.安装babel-plugin-prismjs

npm install babel-plugin-prismjs

3.配置babel.config.js

module.exports = {
  presets: ["@vue/cli-plugin-babel/preset"],
  plugins: [
    // 这块是增加的prismjs的配置
    [
      "prismjs",
      {
        languages: ["javascript", "css", "markup"],
        plugins: ["line-numbers"],
        theme: "okaidia",
        css: true
      }
    ]
  ]
};

 4.vue页面使用

<template>
    <div class="home">
        组件使用说名:
        <!--pre code为使用必须标签 -->
        <pre class="line-numbers">
            <code class="language-xml line-numbers" v-text="html">
            </code>
        </pre>
    </div>
</template>

<script>
import Prism from "prismjs"
export default {
    name: "Home",
    data() {
        return {
            // 这样使用可以保留标签
            html: `<main-content tent :title="$route.meta.title" @titlebar-back="$router.back()" :titleback="true" :loading="loading"></main-content>`
        }
    },
    mounted() {
        // 高亮渲染
        Prism.highlightAll()
    },
    methods: {}
}
</script>

 

 类似资料: