自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

我問導(dǎo)師,Vue3有沒有對應(yīng)工具來生成漂亮的文檔? 用 Vitepress

開發(fā) 開發(fā)工具
Vue3 有沒有對應(yīng)制作文檔的工具。于是,我去查了一些資料,發(fā)現(xiàn),Vue3和新的Vite構(gòu)建工具為我們提供了另一種快速開發(fā)靜態(tài)站點的方法,那就是 Vitepress。今天,我們來一起看看,如何使用 Vitepress 快速為Vue應(yīng)用創(chuàng)建文檔。

[[391287]]

本文已經(jīng)過原作者 Michael Thiessen 授權(quán)翻譯。

最近有人在問:小智, Vue3 有沒有對應(yīng)制作文檔的工具。于是,我去查了一些資料,發(fā)現(xiàn),Vue3和新的Vite構(gòu)建工具為我們提供了另一種快速開發(fā)靜態(tài)站點的方法,那就是 Vitepress。

今天,我們來一起看看,如何使用 Vitepress 快速為Vue應(yīng)用創(chuàng)建文檔。

下面是我們最后要完成的內(nèi)容:

 

在開始之前,我們先來看看 Vitepress 是個啥東西。

Vitepress 是什么?

Vitepress是在Vite之上構(gòu)建的Vue驅(qū)動的靜態(tài)站點生成器。

Vitepress 被稱為“ Vuepress的小弟弟”,它比同類產(chǎn)品具有一些優(yōu)勢。

  • 建立在Vite而非Webpack上,因此啟動時間,熱重裝等更快
  • 使用Vue3來減少JS的有效負(fù)載
  • 輕量級

Vitepress 能夠?qū)崿F(xiàn)這些目標(biāo)的一個原因是,它比Vuepress 更具體,而 Vuepress在過去幾年里變得更加復(fù)雜。

雖然不打算完全取代Vuepress作為 Vue 的靜態(tài)網(wǎng)站生成器,但 Vitepress 提供了一種輕量級的替代方案。對于大多數(shù)項目,例如文檔和簡單站點,Vitepress的特殊性和簡約性將使開發(fā)變得輕而易舉。

創(chuàng)建 Vitepress 項目

首先,創(chuàng)建目錄。

  1. mkdir vite-hello-world 
  2. cd vite-hello-world 

然后,初始化 package 并安裝 Vitepress。

  1. npm init 
  2. npm i --save-dev vitepress 

接著,在package.json中添加一些命令。

  1. // package.json 
  2. "scripts": { 
  3.   "docs:dev""vitepress dev docs"
  4.   "docs:build""vitepress build docs"
  5.   "docs:serve""vitepress serve docs" 
  6. }, 

把上面安排后,再創(chuàng)建一個docs文件夾,制作我們的第一個markdown文件。

  1. mkdir docs 
  2. echo '# Hello World' > docs/index.md 

最后,給它跑起來。

  1. npm run docs:dev~~~~ 

我們已經(jīng)創(chuàng)建了我們的網(wǎng)站,在瀏覽器中打開 http://localhost:3000,則會在網(wǎng)頁中看到我們的markdown文件!

[[391288]]

看到這樣,有點小雞凍了,我們再來看看,如果定制自己想要的東西。

Vitepress 導(dǎo)航

Vitepress 添加多個頁面就像創(chuàng)建更多markdown文件一樣容易。我們在 docs/ 再創(chuàng)建一些文件,如下所示:


Vitepress 創(chuàng)建 SPA 導(dǎo)航時,它將使用每個markdown文件的路徑來創(chuàng)建路由。此外,任何文件夾中名為index.md的文件也只能由/引用。

例如,我們的文件結(jié)構(gòu)轉(zhuǎn)換為以下路由:

  1. // docs/index.md 
  2.  
  3. ## Routing! 
  4.  
  5. [docs/index.md](/) -> / 
  6.  
  7. [docs/contact.md](/contact) -> /contact 
  8.  
  9. [about/index.md](/about/) -> /about/ 
  10.  
  11. [about/our-story.md](/about/our-story) -> /about/our-story 

在我們的md文件中,有三種跳轉(zhuǎn)到路由的方法 我們可以使用基本URL,添加.md或.html-所有這些都將正確鏈接到對應(yīng)的組件。

  1. ### All these options work
  2.  
  3. [docs/contact](/contact) |  
  4. [docs/contact.md](/contact.md) | 
  5. [docs/contact.html](/contact.html) 
圖片

添加導(dǎo)航欄和側(cè)邊欄

Vitepress為我們提供了一個很棒的默認(rèn)主題。它雖然很小,但功能強大且易于定制。

首先,我們通過邊欄和導(dǎo)航欄向我們的網(wǎng)站添加一些導(dǎo)航。

為此,我們需要創(chuàng)建一個配置文件–我們可以在/docs/.vitepress/文件夾中進(jìn)行此操作,該文件夾將存放我們的 Vitepress 特定文件,取名為./vitepress/config.js,只需要導(dǎo)出一個JS對象。

  1. // .vitepress/config.js 
  2.  
  3. module.exports = { 
  4.   title: 'Vitepress Tutorial', // appended to all page titles 

在這個對象中,我們再添加一個themeConfig的屬性

  1. // .vitepress/config.js 
  2. module.exports = { 
  3.   title: "Vitepress Tutorial", // appended to all page titles 
  4.   themeConfig: { 
  5.     nav: [], 
  6.     sidebar: [], 
  7.   }, 
  8. }; 

要將元素添加到導(dǎo)航欄,我們只需要將對象添加到nav數(shù)組中,格式為{text:'ANCHOR-TEXT',link:'PATH'}:

  1. // .vitepress/config.js 
  2. module.exports = { 
  3.   title: "Vitepress Tutorial", // appended to all page titles 
  4.   themeConfig: { 
  5.     nav: [ 
  6.       { text: "Home", link: "/" }, 
  7.       { text: "About", link: "/about/" }, 
  8.       { text: "Contact", link: "/contact" }, 
  9.     ], 
  10.     sidebar: [ 
  11.      { text: 'Our Story', link: '/about/our-story' } 
  12.     ], 
  13.   }, 
  14. }; 

添加側(cè)邊欄也是一樣的方式。

  1. // .vitepress/config.js 
  2. module.exports = { 
  3.   title: "Vitepress Tutorial", // appended to all page titles 
  4.   themeConfig: { 
  5.     nav: [ 
  6.       { text: "Home", link: "/" }, 
  7.       { text: "About", link: "/about/" }, 
  8.       { text: "Contact", link: "/contact" }, 
  9.     ], 
  10.     sidebar: [ 
  11.      { text: 'Our Story', link: '/about/our-story' } 
  12.     ], 
  13.   }, 
  14. }; 

回到我們的瀏覽器,我們現(xiàn)在可以看到 Vitepress 僅從幾行配置就生成了一個非常漂亮的導(dǎo)航欄和側(cè)欄。


Vitepress側(cè)邊欄可以做的一件很酷的事情是根據(jù)我們所在的頁面來更改側(cè)邊欄。

比如,我們想讓首頁顯示其標(biāo)題,其他頁面都顯示我們剛剛制作的側(cè)邊欄。

我們要做的第一件事是創(chuàng)建將our-story側(cè)邊欄存儲為變量。

  1. // .vitepress/config.js 
  2. const primarySidebar = [ 
  3.   { text: 'Our Story', link: '/about/our-story' } 

回到我們的themeConfig對象,我們想將側(cè)邊欄更改為一個對象,其中屬性名稱是路徑,值是側(cè)邊欄數(shù)組。

  1. sidebar: { 
  2.   '/about/': primarySidebar, // everything in the /about/ subdirectory 
  3.   '/contact': primarySidebar, // contact page 

現(xiàn)在,查看瀏覽器,會看到我們的主頁側(cè)邊欄與所有其他邊欄不同。

在Vitepress中內(nèi)置元素

Vitepress 附帶了對幾個元素的支持,我們可以在config.js中或在Markdown中直接聲明這些元素。

這里只會在這里介紹一些最常用的內(nèi)容,完整的內(nèi)容點擊這里。

代碼塊

在編寫好的文檔時,代碼示例至關(guān)重要。Vitepress 提供了一種快速添加代碼塊并指定正在使用的編程語言的方法。



Github樣式表

Vitepress中的表沒接觸過 md 的小伙伴,可能會覺得有點奇怪,如下所示:

  1. // .vitepress/config.js 
  2.  
  3. | Headings      | Are           | Centered    | 
  4. ------------- |:-------------:| -----:      | 
  5. left align    | centered      | right align | 
  6. | zebra striped | rows          | easy        | 

 

Markdown Frontmatter配置

盡管我們可以使用./vuepress/config.js配置網(wǎng)站,但有時我們還是需要對各個頁面進(jìn)行更細(xì)的控制。

幸運的是,我們可以使用markdown文件頂部的YAML塊來控制每個頁面。Vitepress通過用三個虛線(---)包圍它來聲明該塊。

  1. // docs/contact 
  2.  
  3. --- 
  4. title: Contact 
  5. --- 
  6.  
  7. # Contact 

 

我們可以在這里配置很多不同的東西。查看所有Frontmatter選項的文檔。

部署 Vitepress 應(yīng)用

我們使用npm run docs:dev創(chuàng)建本地環(huán)境,但是構(gòu)建用于生產(chǎn)的應(yīng)用又該怎么做呢?

可以使用以下命令來構(gòu)建:

  1. npm run dev:build 

默認(rèn)情況下,構(gòu)建后的文件會被放到/docs/.vuepress/dist下。


作者:Michael Thiessen 譯者:前端小智來源:news原文:https://learn.co/2021/01/write-beautul-documentation-quickly-with-vitepress/

 

責(zé)任編輯:姜華 來源: 大遷世界
相關(guān)推薦

2024-09-05 08:50:11

2021-12-01 08:11:44

Vue3 插件Vue應(yīng)用

2022-06-29 16:59:21

Vue3Vue2面試

2021-05-27 10:36:34

ProvideInjectVue3

2021-11-30 08:19:43

Vue3 插件Vue應(yīng)用

2023-11-28 09:03:59

Vue.jsJavaScript

2021-12-16 08:47:56

Vue3 插件Vue應(yīng)用

2009-02-04 08:52:55

動態(tài)頁面XMLXSL

2021-12-02 05:50:35

Vue3 插件Vue應(yīng)用

2024-05-27 08:39:17

Vue3變量響應(yīng)式

2020-09-19 21:15:26

Composition

2022-07-08 08:52:25

Vue3組合動態(tài)返回

2021-12-08 09:09:33

Vue 3 Computed Vue2

2022-03-07 11:15:25

Pinia狀態(tài)庫vue3

2020-11-12 08:32:14

Vue3模板優(yōu)化

2022-06-21 12:09:18

Vue差異

2022-07-11 10:32:35

Vue3await響應(yīng)式

2022-06-29 08:45:26

Vue3vue 組合ref

2021-05-26 10:40:28

Vue3TypeScript前端

2022-03-10 11:04:04

Vue3Canvas前端
點贊
收藏

51CTO技術(shù)棧公眾號