cmd
命令工具,輸入npm install -g vue-cli
全局安裝 vue-clinpm config set registry https://registry.npm.taobao.org
cmd
,進入想要建立項目的目錄下,輸入:vue init webpack vue-ui-docs
,回車等待初始化完成根據控制檯輸入命令完成接下去的操做javascript
cd C:\Users\jccf\Desktop\vue-ui-docs
,執行命令npm install
cmd
,進入到項目所在目錄下,輸入npm run dev
,回車,啓動項目http://localhost:8080
訪問├─examples // 原 src 目錄,改爲 examples 用做示例展現 │ │ App.vue //主頁文件 │ │ main.js //項目入口文件 │ │ │ ├─docs //markdown幫助文檔文件夾 │ │ │ └─router │ index.js //路由配置文件 │ ├─src // 新增 src 用於編寫存放組件 │ │ index.js //組件集成統一訪問文件配置 │ │ │ ├─components //組件文件夾 │ ├─directives //自定義指令文件夾 │ ├─mixins //混入文件 │ └─styles //樣式文件夾
因爲目錄調整須要調整對應的webpack
配置才能夠啓動項目css
examples\router\index.js
的無效代碼\build\webpack.base.conf.js
文件進行配置調整webpack
的主文件入口entry: { app: './examples/main.js', }
webpack
編譯配置{ test: /\.js$/, loader: 'babel-loader', include: [resolve('examples'), resolve('src')] }
App.vue
的代碼和引用npm run dev
有錯誤就根據錯誤調整,直到能夠正常訪問不在報錯markdown
文件經過markdown
寫幫助文檔而後解析爲頁面,參考餓了麼UI
組件庫的使用 vue-markdown-loader
將markdown
文件解析爲vue
組件直接頁面渲染,安裝vue-markdown-loader
直接執行命令html
npm install vue-markdown-loader --save-devvue
配置webpack
加載器解析markdown
,在vue-loader.conf.js
在屬性rules
追加配置加載器java
module.exports = { module: { rules: [ { test: /\.md$/, loader: 'vue-markdown-loader' } ] } };
docs
文件下建立第一個markdown
文件,test.md
# test > Hello World
routes: [{ path: '/test', name: 'test', component: r => require.ensure([], () => r(require('../docs/test.md'))) }]
http://localhost:8080/#/test
,正確的將markdown
解析爲 vue 組件並正確初始化路由:::demo ### 描述標題 ```html <template> <img src="https://i.loli.net/2017/08/21/599a521472424.jpg" /> </template> <script> console.log(1) </script> ``` ::: ```
markdown
編譯成如下效果上面爲代碼執行示例
,中間爲描述信息
,底部爲代碼示例
webpack
demo-block
用於顯示代碼塊的組件<template> <div class="demo-block"> <div class="demo-block-source"> <slot name="source"></slot> <span class="demo-block-code-icon" v-if="!$slots.default" @click="showCode=!showCode"><img alt="expand code" src="https://gw.alipayobjects.com/zos/rmsportal/wSAkBuJFbdxsosKKpqyq.svg" class="code-expand-icon-show"></span> </div> <div class="demo-block-meta" v-if="$slots.default"> <slot></slot> <span v-if="$slots.default" class="demo-block-code-icon" @click="showCode=!showCode"><img alt="expand code" src="https://gw.alipayobjects.com/zos/rmsportal/wSAkBuJFbdxsosKKpqyq.svg" class="code-expand-icon-show"></span> </div> <div class="demo-block-code" v-show="showCode"> <slot name="highlight"></slot> </div> </div> </template> <script type="text/babel"> export default { data() { return { showCode: false }; } }; </script> <style> .demo-block { border: 1px solid #ebedf0; border-radius: 2px; display: inline-block; width: 100%; position: relative; margin: 0 0 16px; -webkit-transition: all 0.2s; transition: all 0.2s; border-radius: 2px; } .demo-block p { padding: 0; margin: 0; } .demo-block .demo-block-code-icon { position: absolute; right: 16px; bottom: 14px; cursor: pointer; width: 18px; height: 18px; line-height: 18px; text-align: center; } .demo-block .demo-block-code-icon img { -webkit-transition: all 0.4s; transition: all 0.4s; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; position: absolute; left: 0; top: 0; margin: 0; max-width: 100%; width: 100%; vertical-align: baseline; -webkit-box-shadow: none; box-shadow: none; } .demo-block .demo-block-source { border-bottom: 1px solid #ebedf0; padding: 20px 24px 20px; color: #444; position: relative; margin-bottom: -1px; } .demo-block .demo-block-meta { position: relative; padding: 12px 50px 12px 20px; border-radius: 0 0 2px 2px; -webkit-transition: background-color 0.4s; transition: background-color 0.4s; width: 100%; -webkit-box-sizing: border-box; box-sizing: border-box; font-size: 14px; color: #444; font-size: 14px; line-height: 2; border-radius: 0; border-bottom: 1px dashed #ebedf0; margin-bottom: -1px; } .demo-block .demo-block-meta code { color: #444; background-color: #e6effb; margin: 0 4px; display: inline-block; padding: 3px 7px; border-radius: 3px; height: 18px; line-height: 18px; font-family: Menlo, Monaco, Consolas, Courier, monospace; font-size: 14px; } .demo-block .demo-block-code { background-color: #f7f7f7; font-size: 0; } .demo-block .demo-block-code code { background-color: #f7f7f7; font-family: Consolas, Menlo, Courier, monospace; border: none; display: block; font-size: 14px; padding: 16px 32px; } .demo-block .demo-block-code pre { margin: 0; padding: 0; } .sh-checkbox { color: #444; font-weight: 500; font-size: 14px; position: relative; cursor: pointer; display: inline-block; white-space: nowrap; user-select: none; } </style>
vue-markdown-loader
依賴了highlight
在App.vue
的樣式中引用進行代碼着色,風格參照highlight.js
本身引用@import 'highlight.js/styles/color-brewer.css';
main.js
配置全局安裝組件,讓每一個md
文件均可以自動編譯成 vue 組件而且渲染代碼塊import DemoBlock from './components/demo-block.vue' Vue.component('demo-block', DemoBlock)
webpack.base.conf.js
配置vue-markdown-loader
的options
屬性
demo
代碼塊解析,在 markdown 用demo-block
組件包裹npm install markdown-it-container --save-dev
const markdownRender = require('markdown-it')(); { test: /\.md$/, loader: 'vue-markdown-loader', options: { preventExtract: true, use: [ [require('markdown-it-container'), 'demo', { validate: function (params) { return params.trim().match(/^demo\s+(.*)$/); }, render: function (tokens, idx) { if (tokens[idx].nesting === 1) { // 1.獲取第一行的內容使用markdown渲染html做爲組件的描述 let demoInfo = tokens[idx].info.trim().match(/^demo\s+(.*)$/); let description = (demoInfo && demoInfo.length > 1) ? demoInfo[1] : ''; let descriptionHTML = description ? markdownRender.render(description) : ''; // 2.獲取代碼塊內的html和js代碼 let content = tokens[idx + 1].content; // 3.使用自定義開發組件【DemoBlock】來包裹內容而且渲染成案例和代碼示例 return `<demo-block> <div class="source" slot="source">${content}</div> ${descriptionHTML} <div class="highlight" slot="highlight">`; } else { return '</div></demo-block>\n'; } } }] ] } }
從新運行npm run dev
獲得預期的效果git
對App.vue
樣式和排版佈局進行調整
github
src\components
下開發kt-button.vue
按鈕組件<template> <div class="kt-button"> <slot></slot> </div> </template> <script> export default { name: 'KtButton' } </script> <style> .kt-button { border: 1px solid #41a259; background-color: #41a259; display: inline-block; border-radius: 2px; height: 14px; line-height: 14px; color: #fff; padding: 10px 19px; cursor: pointer; white-space: nowrap; } </style>
examples\docs
下建立button.md
進行文檔編寫和代碼示例# Button 按鈕 ## 基礎用法 :::demo 經過`plain`屬性能夠設置爲樸素的按鈕 ```html <kt-button>確認</kt-button> ``` ::: ```
export default new Router({ routes: [ { path: '/test', name: 'test', component: r => require.ensure([], () => r(require('../docs/test.md'))) }, { path: '/button', name: 'button', component: r => require.ensure([], () => r(require('../docs/button.md'))) } ] })
<router-link to="/button">button 組件</router-link>
src
下的的index.js
import KtButton from './components/kt-button.vue' const components = [KtButton] export default function(Vue) { components.map(component => { Vue.component(component.name, component) }) }
main.js
安裝全部的組件,讓全部的docs
下的md
文件均可以調用import install from '../src/index' install(Vue)
代碼下載:githubweb
示例演示: shui.kitorv.comvue-router