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

开发
前天,我写了一篇简短的文章,描述了如何创建一个简单的 Vue JS 应用程序,而不需要任何构建。人们对此很感兴趣,并给予了很多反馈。许多读者问,在即将发布的 Vue 3版本中,是否可能出现类似的壮举。答案是肯定的。而且,使用 Vue 3,你可以走得更远,不需要任何构建过程就可以享受渐进式 web 框架的力量。

关于 Vue 2版本的原始文章可以在 https://letsdebug.it/post/minimalistic-Vue 网站上找到。下面我们将描述如何使用 Vue 3实现类似的设置

[[342745]]

这篇文章的源代码可以在 https://bitbucket.org/letsdebugit/minimalistic-vue-3中找到,你可以在这里运行这个示例应用程序

应用程序设计
与 Vue 2的例子类似,我们将创建一个带有页眉、内容区域和页脚的单页面 web 应用程序。在内容区域有一条消息和一个按钮。当用户单击按钮时,消息将发生变化。UI 由定制 HTML 标记表示的 Vue 组件构成。

工程项目结构
该项目的结构与 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 组件清楚地反映在项目的目录结构中。在组件的代码中有一些变化,如下所述。

自力更生
当浏览器加载 index. html 时,会发生以下情况:

vue3.0库是从 CDN 仓库获取的https://unpkg.com/vue@3.0.0-rc.8
获取组件样式
应用程序模块从index.js开始然后被执行
请注意,在编写 Vue 3的时候,Vue 3还没有正式发布。因此,我们在这里使用最新的可用版本3.0.0-rc. 8。当官方发布时,你将不得不相应地改变 URL。

当执行 index.js 时,它导入并注册包含我们的组件的后续模块:

  1. Content from 内容来自/content/content.js 
  2. Header from 标题来自/header/header.js 
  3. Footer from 的页脚/footer/footer.js 

最后,它创建应用程序实例,并将其挂载到index.html内的<main>标记中。

组件
有了这个框架的新版本,我们可以利用新的函数式编程模型,也就是复合 API。我们将使用 setup()函数来代替数据、计算和方法部分,它将连接所有组件的内部。为了确保数据传播到 UI 并对更改做出反应,我们将使用 composition api 提供的reactive 和 computed。

组件代码的结构如下:

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

作为一个例子,我们提供了 footer 组件,它在左边显示一些文本,在右边显示一个滴答作响的时钟:

  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.   } 

主要的应用程序组件在 index.js 文件中。它的任务是为所有组件分配定制的 HTML 标记,比如 < 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. }) 

然后使用这些自定义标记在 index. html 文件中构建应用程序 UI。我们最终得到了一个简单易懂的用户界面:

  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,而且没有任何构建过程的复杂性。要部署这个应用程序,我们只需将文件复制到一个 web 服务器。

 

责任编辑:姜华 来源: 小丑的小屋
相关推荐

2024-03-01 11:32:22

Vue3APIVue.js

2023-01-28 13:34:47

Web 3.0区块链互联网

2020-08-05 11:53:41

数据代码自动化

2015-09-30 09:57:53

天分热情工程师

2020-10-13 08:24:31

Vue3.0系列

2017-03-13 13:54:40

戴尔

2018-05-07 14:11:15

RootAndroidXposed

2012-08-23 09:50:07

测试测试人员软件测试

2023-10-16 07:42:10

前端构建高性能

2009-11-23 12:45:22

2021-04-02 10:30:18

Vue3.0前端代码

2019-07-17 06:17:01

UbuntuUbuntu LTSNvidia驱动

2022-08-22 15:10:38

JSCSS页面滚动

2022-06-07 17:01:31

UI框架前端

2020-07-28 08:28:07

JavaScriptswitch开发

2015-08-20 10:56:19

算法界面开发

2013-12-02 09:43:29

字符串编程

2024-02-22 09:00:00

LogitMat数据集算法

2018-01-29 13:18:42

前端JavaScript

2010-11-23 10:55:47

跳槽
点赞
收藏

51CTO技术栈公众号