Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【实践相关】Vue集成highlight.js #40

Open
guapi233 opened this issue Nov 16, 2020 · 0 comments
Open

【实践相关】Vue集成highlight.js #40

guapi233 opened this issue Nov 16, 2020 · 0 comments
Labels
工具库使用 对工具库的使用经验

Comments

@guapi233
Copy link
Owner

Vue集成highlight.js

一、安装highlight.js

npm install highlight.js --save 或 yarn add highlight.js

二、封装自定义指令

import Hljs from "highlight.js";

export const highlight = (el: any) => {
  const blocks = el.querySelectorAll("pre code");

  blocks.forEach((block: any) => {
    Hljs.highlightBlock(block);
  });
};

三、引入指令和样式

import { highlight } from "@/utils/directive";
Vue.directive("highlight", highlight);

// 样式文件在 node_modules/highlight.js/style/ 中可以找到
@import "../assets/atom-one-dark";

四丶使用指令

<div v-highlight v-html="articleDetail.content"></div>
@guapi233 guapi233 added the 工具库使用 对工具库的使用经验 label Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
工具库使用 对工具库的使用经验
Projects
None yet
Development

No branches or pull requests

1 participant