美文网首页
highlight.js 代码高亮

highlight.js 代码高亮

作者: _花 | 来源:发表于2022-02-28 16:44 被阅读0次

    如何在VUE2.0项目里使用highlight

    1.highlight 10.*版本的

    按需引入高亮代码后缀
    index.js

    const hljs = require('highlight.js/lib');
    hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));
    hljs.registerLanguage('json', require('highlight.js/lib/languages/json'));
    
    module.exports = hljs;
    

    然后在main.js中注册

    // 代码高亮
    import {vuePlugin} from '@/utils/hightLight/index.js';
    import 'highlight.js/styles/atom-one-dark-reasonable.css';
    console.log(vuePlugin);
    Vue.use(vuePlugin);
    

    2.highlight 11.*版本的

    下载@highlightjs/vue-plugin
    未按需引入

    import 'highlight.js/lib/common';
    import 'highlight.js/styles/stackoverflow-light.css'
    import hljsVuePlugin from "@highlightjs/vue-plugin";
    
    const app = createApp(App)
    Vue.use(hljsVuePlugin)
    

    参考链接:https://github.com/highlightjs/vue-plugin
    3.任何版本
    npm install --save vue-highlightjs
    main.js

    // Import Vue and vue-highlgihtjs
    import Vue from 'vue'
    import VueHighlightJS from 'vue-highlightjs'
    import 'highlight.js/styles/default.css' // or other highlight.js theme
    
    // Tell Vue.js to use vue-highlightjs
    Vue.use(VueHighlightJS)
    
    <!-- If your source-code lives in a variable called 'sourcecode' -->
    <pre v-highlightjs="sourcecode"><code class="javascript"></code></pre>
    
    <!-- If you want to highlight hardcoded source-code -->
    <pre v-highlightjs><code class="javascript">const s = new Date().toString()</code></pre>
    

    参考链接:https://github.com/metachris/vue-highlightjs

    相关文章

      网友评论

          本文标题:highlight.js 代码高亮

          本文链接:https://www.haomeiwen.com/subject/jkilrrtx.html