{"id":1053,"date":"2023-03-12T10:09:43","date_gmt":"2023-03-12T02:09:43","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1053"},"modified":"2023-04-29T12:39:34","modified_gmt":"2023-04-29T04:39:34","slug":"using-highlight-js-in-vue","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/using-highlight-js-in-vue\/","title":{"rendered":"Vue\u4e2d\u4f7f\u7528highlight.js"},"content":{"rendered":"<p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u6307\u4ee4\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5728Vue\u4e2d\u5b9e\u73b0\u8bed\u6cd5\u9ad8\u4eae<\/p>\n<h2>\u57fa\u672c\u5b9e\u73b0<\/h2>\n<ul>\n<li>\u5b89\u88c5<\/li>\n<\/ul>\n<pre><code>npm install highlight.js --save<\/code><\/pre>\n<p><!-- more --><\/p>\n<ul>\n<li>\u7f16\u7801<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">\/\/ Vue-cli\u751f\u6210\u7684\u5de5\u7a0b\u6587\u4ef6\u7684src\/main.js\nimport hljs from &#039;highlight.js&#039;\nimport &#039;highlight.js\/styles\/googlecode.css&#039; \/\/\u6837\u5f0f\u6587\u4ef6\n\nVue.directive(&#039;highlight&#039;, function (el) {\n  let blocks = el.querySelectorAll(&#039;pre code&#039;);\n  blocks.forEach((block) =&gt; {\n    hljs.highlightBlock(block)\n  })\n})<\/code><\/pre>\n<ul>\n<li>\u4f7f\u7528<\/li>\n<\/ul>\n<pre><code class=\"language-html\">&lt;div v-html=&quot;markdownhtml&quot; v-highlight&gt;&lt;\/div&gt;<\/code><\/pre>\n<h2>\u5c01\u88c5\u6210\u63d2\u4ef6<\/h2>\n<ul>\n<li>\u7f16\u5199\u63d2\u4ef6<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">\/\/ highlight.js\nimport Vue from &#039;vue&#039;\nimport Hljs from &#039;highlight.js&#039;\nimport &#039;highlight.js\/styles\/googlecode.css&#039;\n\nlet Highlight = {}\nHighlight.install = function (Vue, options) {\n  Vue.directive(&#039;highlight&#039;, function (el) {\n    let blocks = el.querySelectorAll(&#039;pre code&#039;);\n    blocks.forEach((block) =&gt; {\n      Hljs.highlightBlock(block)\n    })\n  })\n}\n\nexport default Highlight<\/code><\/pre>\n<ul>\n<li>\u4f7f\u7528\u63d2\u4ef6<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">import Highlight from &#039;path\/to\/Highlight.js&#039;\nVue.use(Highlight)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u6307\u4ee4\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5728Vue\u4e2d\u5b9e\u73b0\u8bed\u6cd5\u9ad8\u4eae \u57fa\u672c\u5b9e\u73b0 \u5b89\u88c5 npm install highlight. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[289],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-vue","tag-highlight-js"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}