--- title: 前端 author: pengzhanbo icon: icon-park-outline:html-five createTime: 2024/04/04 11:39:05 permalink: /guide/repl/frontend/ --- ## 概述 前端代码演示 由 [vuepress-plugin-md-enhance](https://plugin-md-enhance.vuejs.press/zh/) 提供支持。 前端 代码演示 默认不启用,你可以通过配置来启用它。 ::: code-tabs @tab .vuepress/config.ts ```ts export default defineUserConfig({ theme: plumeTheme({ plugins: { markdownEnhance: { demo: true, }, } }) }) ``` ::: ## 语法 ```` md ::: [类型]-demo 可选的标题文字 ```html ``` ```json // json block 作为插件配置存放处 { // 放置你的配置 (可选的) } ``` ::: ```` ::: tip 提示 JSON 块是可选的,可用的配置详见[配置](https://vuepress-theme-hope.github.io/v2/md-enhance/zh/config.html) ::: 插件支持三种类型 - normal(默认) - vue - react ## 可用的语言 你可以在演示块中使用不同语言。 当你设置一些不能在浏览器上直接运行的语言时,由于插件无法解析它们,因此网页演示将被禁用。插件只显示代码。同时提供一个 "在 CodePen 中打开" 按钮允许用户直接在 CodePen 打开并浏览代码。 可用的 HTML 语言: - `"html"` (默认) - `"slim"` - `"haml"` - `"markdown"` 可用的 JS 语言: - `"javascript"` (default) - `"coffeescript"` - `"babel"` - `"livescript"` - `"typescript"` > 你也可以在代码块中使用 `js`, `ts`, `coffee` 和 `ls。` 可用的 CSS 语言: - `"css"` (default) - `"less"` - `"scss"` - `"sass"` - `"stylus"` ## 不支持的语言 ::: normal-demo 一个使用浏览器不支持解析语言 Demo ```md # 标题 十分强大 ``` ```ts const message: string = 'VuePress Theme Hope' document.querySelector('h1').innerHTML = message ``` ```scss h1 { font-style: italic; + p { color: red; } } ``` ::: :::: details 代码 ```` md ::: normal-demo 一个使用浏览器不支持解析语言 Demo ```md # 标题 十分强大 ``` ```ts const message: string = 'VuePress Theme Hope' document.querySelector('h1').innerHTML = message ``` ```scss h1 { font-style: italic; + p { color: red; } } ``` ::: ```` :::: ## 普通代码演示 格式: ```` md ::: normal-demo 可选的标题文字 ```html ``` ```js // js code ``` ```css /* css code */ ``` ```json // 配置 (可选) { "jsLib": [ // ... ], "cssLib": [ // ... ] } ``` ::: ```` ::: warning 注意事项 我们使用 `"ShadowDOM"` 进行样式隔离,并已经将 `document` 替换为了 `shadowRoot` 对象。如果需要访问页面的 `document,请访问` `window.document`。 ::: ## 例子 ::: normal-demo Demo 演示 ```html
非常强大!
``` ```js document.querySelector('#very').addEventListener('click', () => { alert('非常强大') }) ``` ```css span { color: red; } ``` ::: :::: details 代码 ```` md ::: normal-demo Demo 演示 ```html非常强大!
``` ```js document.querySelector('#very').addEventListener('click', () => { alert('非常强大') }) ``` ```css span { color: red; } ``` ::: ```` :::: ### Vue 代码演示 #### 格式 ```` md ::: vue-demo 可选的标题文字 ```vueHello Word is
{{ message }}!
Hello Word is
{{ message }}!
Hello Word !
{message}
Hello Word !
{message}