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

使用vue3.0,不需要build也可以

開發(fā)
前天,我寫了一篇簡短的文章,描述了如何創(chuàng)建一個(gè)簡單的 Vue JS 應(yīng)用程序,而不需要任何構(gòu)建。人們對此很感興趣,并給予了很多反饋。許多讀者問,在即將發(fā)布的 Vue 3版本中,是否可能出現(xiàn)類似的壯舉。答案是肯定的。而且,使用 Vue 3,你可以走得更遠(yuǎn),不需要任何構(gòu)建過程就可以享受漸進(jìn)式 web 框架的力量。

關(guān)于 Vue 2版本的原始文章可以在 https://letsdebug.it/post/minimalistic-Vue 網(wǎng)站上找到。下面我們將描述如何使用 Vue 3實(shí)現(xiàn)類似的設(shè)置

[[342745]]

這篇文章的源代碼可以在 https://bitbucket.org/letsdebugit/minimalistic-vue-3中找到,你可以在這里運(yùn)行這個(gè)示例應(yīng)用程序

應(yīng)用程序設(shè)計(jì)
與 Vue 2的例子類似,我們將創(chuàng)建一個(gè)帶有頁眉、內(nèi)容區(qū)域和頁腳的單頁面 web 應(yīng)用程序。在內(nèi)容區(qū)域有一條消息和一個(gè)按鈕。當(dāng)用戶單擊按鈕時(shí),消息將發(fā)生變化。UI 由定制 HTML 標(biāo)記表示的 Vue 組件構(gòu)成。

工程項(xiàng)目結(jié)構(gòu)
該項(xiàng)目的結(jié)構(gòu)與 Vue 2版本完全相同: 

  1. index.html 
  2. index.js 
  3. index.css 
  4. header/ 
  5.     header.js 
  6.     header.css 
  7. content/ 
  8.     content.js 
  9.     content.css 
  10. footer/ 
  11.     footer.js 
  12.     footer.css 

我們的邏輯 UI 組件清楚地反映在項(xiàng)目的目錄結(jié)構(gòu)中。在組件的代碼中有一些變化,如下所述。

自力更生
當(dāng)瀏覽器加載 index. html 時(shí),會發(fā)生以下情況:

vue3.0庫是從 CDN 倉庫獲取的https://unpkg.com/vue@3.0.0-rc.8
獲取組件樣式
應(yīng)用程序模塊從index.js開始然后被執(zhí)行
請注意,在編寫 Vue 3的時(shí)候,Vue 3還沒有正式發(fā)布。因此,我們在這里使用最新的可用版本3.0.0-rc. 8。當(dāng)官方發(fā)布時(shí),你將不得不相應(yīng)地改變 URL。

當(dāng)執(zhí)行 index.js 時(shí),它導(dǎo)入并注冊包含我們的組件的后續(xù)模塊:

  1. Content from 內(nèi)容來自/content/content.js 
  2. Header from 標(biāo)題來自/header/header.js 
  3. Footer from 的頁腳/footer/footer.js 

最后,它創(chuàng)建應(yīng)用程序?qū)嵗⑵鋻燧d到index.html內(nèi)的<main>標(biāo)記中。

組件
有了這個(gè)框架的新版本,我們可以利用新的函數(shù)式編程模型,也就是復(fù)合 API。我們將使用 setup()函數(shù)來代替數(shù)據(jù)、計(jì)算和方法部分,它將連接所有組件的內(nèi)部。為了確保數(shù)據(jù)傳播到 UI 并對更改做出反應(yīng),我們將使用 composition api 提供的reactive 和 computed。

組件代碼的結(jié)構(gòu)如下:

  1. const template = ` 
  2.   <div> 
  3.   ... 
  4.   </div> 
  5. export default { 
  6.   template, 
  7.   setup () { 
  8.   } 

作為一個(gè)例子,我們提供了 footer 組件,它在左邊顯示一些文本,在右邊顯示一個(gè)滴答作響的時(shí)鐘:

  1. const { reactive, computed } = Vue 
  2. const template = ` 
  3.   <footer> 
  4.     <div class="left"
  5.       <slot></slot> 
  6.     </div> 
  7.     <div class="middle"
  8.     </div> 
  9.     <div class="right"
  10.       Current time: <b>{{ state.nowString }}</b> 
  11.     </div> 
  12.   </footer> 
  13. export default { 
  14.   template, 
  15.   setup () { 
  16.     const state = reactive({ 
  17.       now: new Date(), 
  18.       nowString: computed(() => state.now.toTimeString().substr(0, 8)) 
  19.     }) 
  20.     window.setInterval(() => { 
  21.       state.now = new Date() 
  22.     }, 1000) 
  23.     return { state } 
  24.   } 

主要的應(yīng)用程序組件在 index.js 文件中。它的任務(wù)是為所有組件分配定制的 HTML 標(biāo)記,比如 < app-header > 或 < app-footer > 。

  1. import Header from './header/header.js' 
  2. import Content from './content/content.js' 
  3. import Footer from './footer/footer.js' 
  4. const { createApp } = Vue 
  5. const App = createApp({ 
  6.   components: { 
  7.     'app-header': Header, 
  8.     'app-content': Content, 
  9.     'app-footer': Footer 
  10.   } 
  11. window.addEventListener('load', () => { 
  12.   App.mount('main'
  13. }) 

然后使用這些自定義標(biāo)記在 index. html 文件中構(gòu)建應(yīng)用程序 UI。我們最終得到了一個(gè)簡單易懂的用戶界面:

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.   <meta charset="utf-8"
  5.   <title>Minimalistic Vue 3</title> 
  6.   <link rel="stylesheet" href="index.css"
  7.   <link rel="stylesheet" href="header/header.css"
  8.   <link rel="stylesheet" href="content/content.css"
  9.   <link rel="stylesheet" href="footer/footer.css"
  10.   <script src="https://unpkg.com/vue@3.0.0-rc.8"></script> 
  11.   <script src="index.js" type="module"></script> 
  12. </head> 
  13. <body> 
  14.   <main> 
  15.     <app-header bg-color="#c5cae2"
  16.     </app-header> 
  17.     <app-content> 
  18.     </app-content> 
  19.     <app-footer> 
  20.       (c) Tomasz Waraksa, Dublin, Ireland 
  21.     </app-footer> 
  22.   </main> 
  23. </body> 
  24. </html> 

最后,我們幾乎擁有了 Vue 3的全部功能,包括了不起的 Composition API,而且沒有任何構(gòu)建過程的復(fù)雜性。要部署這個(gè)應(yīng)用程序,我們只需將文件復(fù)制到一個(gè) web 服務(wù)器。

 

責(zé)任編輯:姜華 來源: 小丑的小屋
相關(guān)推薦

2024-03-01 11:32:22

Vue3APIVue.js

2023-01-28 13:34:47

Web 3.0區(qū)塊鏈互聯(lián)網(wǎng)

2020-10-13 08:24:31

Vue3.0系列

2020-08-05 11:53:41

數(shù)據(jù)代碼自動(dòng)化

2015-09-30 09:57:53

天分熱情工程師

2017-03-13 13:54:40

戴爾

2018-05-07 14:11:15

RootAndroidXposed

2021-04-02 10:30:18

Vue3.0前端代碼

2009-11-23 12:45:22

2012-08-23 09:50:07

測試測試人員軟件測試

2023-10-16 07:42:10

前端構(gòu)建高性能

2022-02-06 22:13:47

VueVue3.0Vue項(xiàng)目

2020-08-25 09:50:35

Vue3.0命令前端

2019-07-17 06:17:01

UbuntuUbuntu LTSNvidia驅(qū)動(dòng)

2022-08-22 15:10:38

JSCSS頁面滾動(dòng)

2020-07-28 08:28:07

JavaScriptswitch開發(fā)

2022-06-07 17:01:31

UI框架前端

2013-07-18 09:21:32

代碼文檔

2022-02-15 07:26:34

web前端算法題

2010-11-23 10:55:47

跳槽
點(diǎn)贊
收藏

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