美文网首页
【Vue3+Vite3】使用 Vite3 构建 demo 项目

【Vue3+Vite3】使用 Vite3 构建 demo 项目

作者: MasonChan | 来源:发表于2023-02-22 19:37 被阅读0次

初始化 demo 项目

npm init vue@latest

这个命令的意思是:npm init 使用 vite3 工具进行 vue 项目构建。

执行命令后会询问是否需要安装依赖,对于新手来说,按回车跳过即可,以后用到的时候再安装。

因为 Vue3 官方文档 对于 JavaScript 基础薄弱的初学者来说,显得过于深奥了,对于没有用过 vue cli 构建工程的人来说也很难懂,下面从初始化 demo 项目后,说一说新手需要关心的文件及内容:

目录 说明
package.json 项目运行环境信息
package-lock.json 依赖模块的信息
vite.config.js vite 的 web 服务配置
index.html 首页,与 src/main.js 产生关联
src/main.js 应用程序入口文件,与 src/App.vue 产生关联
src/App.vue 应用的根组件,与 components/HellowWorld.vue 和 components/WelComeItem.vue 产生关联
components/HellowWorld.vue 子组件 HellowWorld
components/WelComeItem.vue 子组件 WelComeItem,与 components/TheWelcome.vue 和 其他资源产生关联
components/TheWelcome.vue 子组件 TheWelcome
components/icons/*.vue 图标组件代码
assets/* 公共 CSS 样式代码
public/* 公共资源目录

package.json

项目运行环境信息,执行 npm run cmd --args 用到

{
  // 项目信息
  "name": "mydemo",
  "version": "0.0.0",
  "private": true,
  // scripts 的 keys 是 npm run cmd 中的 cmd,
  // 例如 npm run build 等同于 npm run vite build
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  // 依赖的 vue 版本
  "dependencies": {
    "vue": "^3.2.45"
  },
  // 开发环境依赖的 vite 版本
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.0.0",
    "vite": "^4.0.0"
  }

package-lock.json

所有依赖模块的信息,执行模块安装命令 npm install 时自动生成,删掉不影响运行

vite.config.js

vite 的 web 服务运行配置

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/ ,建议切到中文版进行学习
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

删除这个文件后,vite 可以正常启动,但是访问 url 会报错:

Failed to parse source for import analysis because the content contains invalid JS syntax.

index.html

首页,引用 src/main.js 文件,使得 index.html 与 vue 产生关联

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vite App</title>
  </head>
  <body>
    <!-- 定义一个 id 为 app 的组件 -->
    <div id="app"></div>
    <!-- 引用 js 文件,使得 index.html 与 vue 产生关联 -->
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

src/main.js

应用程序入口文件

// 导入 vue 框架中的 createApp 方法
import { createApp } from 'vue'
// 导入用户自定义的根组件
import App from './App.vue'
// 导入 css 样式文件,全局生效
import './assets/main.css'
// 挂载根组件,并渲染到 index.html 中 id 为 app 的 HTML 标签中
createApp(App).mount('#app')

src/App.vue

用户应用的根组件,看注释就好,代码可以先不看

<script setup>
<!-- 导入子组件 -->
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
</script>
<!-- 将 id 为 app 的 div 标签替换为以下 HTML 内容 -->
<template>
  <header>
    <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />

    <div class="wrapper">
      <!-- 使用 HelloWorld 组件,这个组件有个参数叫 args -->
      <HelloWorld msg="You did it!" />
    </div>
  </header>

  <main>
    <!-- 使用 TheWelcome 组件 -->
    <TheWelcome />
  </main>
</template>
<!-- 只对本组件生效的 CSS 样式 -->
<style scoped>
header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
}
</style>

components/HellowWorld.vue

子组件 HellowWorld,看注释就好,代码可以先不看

<script setup>
// 定义组件的参数:msg,数据类型为 String,使用此组件时必须提供 msg 参数的内容
defineProps({
  msg: {
    type: String,
    required: true
  }
})
</script>
<!-- 此组件的 HTML 内容 -->
<template>
  <div class="greetings">
    <h1 class="green">{{ msg }}</h1>
    <h3>
      You’ve successfully created a project with
      <a href="https://vitejs.dev/" target="_blank" rel="noopener">Vite</a> +
      <a href="https://vuejs.org/" target="_blank" rel="noopener">Vue 3</a>.
    </h3>
  </div>
</template>
<!-- 仅对此组件生效的的 CSS 样式 -->
<style scoped>
h1 {
  font-weight: 500;
  font-size: 2.6rem;
  top: -10px;
}

h3 {
  font-size: 1.2rem;
}

.greetings h1,
.greetings h3 {
  text-align: center;
}

@media (min-width: 1024px) {
  .greetings h1,
  .greetings h3 {
    text-align: left;
  }
}
</style>

src/TheWelcome.vue

子组件 TheWelcome,看注释就好,代码可以先不看

<script setup>
// 引入 WelComeItem 组件和图标组件 icons/*.vue
import WelcomeItem from './WelcomeItem.vue'
import DocumentationIcon from './icons/IconDocumentation.vue'
import ToolingIcon from './icons/IconTooling.vue'
import EcosystemIcon from './icons/IconEcosystem.vue'
import CommunityIcon from './icons/IconCommunity.vue'
import SupportIcon from './icons/IconSupport.vue'
</script>
<!-- 本组件的 HTML 内容 -->
<template>
  <!-- 使用 WelComeItem 组件 -->
  <WelcomeItem>
    <template #icon>
      <DocumentationIcon />
    </template>
    <template #heading>Documentation</template>

    Vue’s
    <a href="https://vuejs.org/" target="_blank" rel="noopener">official documentation</a>
    provides you with all information you need to get started.
  </WelcomeItem>
  <!-- 使用 WelComeItem 组件 -->
  <WelcomeItem>
    <template #icon>
      <ToolingIcon />
    </template>
    <template #heading>Tooling</template>

    This project is served and bundled with
    <a href="https://vitejs.dev/guide/features.html" target="_blank" rel="noopener">Vite</a>. The
    recommended IDE setup is
    <a href="https://code.visualstudio.com/" target="_blank" rel="noopener">VSCode</a> +
    <a href="https://github.com/johnsoncodehk/volar" target="_blank" rel="noopener">Volar</a>. If
    you need to test your components and web pages, check out
    <a href="https://www.cypress.io/" target="_blank" rel="noopener">Cypress</a> and
    <a href="https://on.cypress.io/component" target="_blank">Cypress Component Testing</a>.

    <br />

    More instructions are available in <code>README.md</code>.
  </WelcomeItem>
  <!-- 使用 WelComeItem  组件 -->
  <WelcomeItem>
    <template #icon>
      <EcosystemIcon />
    </template>
    <template #heading>Ecosystem</template>

    Get official tools and libraries for your project:
    <a href="https://pinia.vuejs.org/" target="_blank" rel="noopener">Pinia</a>,
    <a href="https://router.vuejs.org/" target="_blank" rel="noopener">Vue Router</a>,
    <a href="https://test-utils.vuejs.org/" target="_blank" rel="noopener">Vue Test Utils</a>, and
    <a href="https://github.com/vuejs/devtools" target="_blank" rel="noopener">Vue Dev Tools</a>. If
    you need more resources, we suggest paying
    <a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">Awesome Vue</a>
    a visit.
  </WelcomeItem>
  <!-- 使用 WelComeItem 组件 -->
  <WelcomeItem>
    <template #icon>
      <CommunityIcon />
    </template>
    <template #heading>Community</template>

    Got stuck? Ask your question on
    <a href="https://chat.vuejs.org" target="_blank" rel="noopener">Vue Land</a>, our official
    Discord server, or
    <a href="https://stackoverflow.com/questions/tagged/vue.js" target="_blank" rel="noopener"
      >StackOverflow</a
    >. You should also subscribe to
    <a href="https://news.vuejs.org" target="_blank" rel="noopener">our mailing list</a> and follow
    the official
    <a href="https://twitter.com/vuejs" target="_blank" rel="noopener">@vuejs</a>
    twitter account for latest news in the Vue world.
  </WelcomeItem>
  <!-- 使用 WelComeItem 组件 -->
  <WelcomeItem>
    <template #icon>
      <SupportIcon />
    </template>
    <template #heading>Support Vue</template>

    As an independent project, Vue relies on community backing for its sustainability. You can help
    us by
    <a href="https://vuejs.org/sponsor/" target="_blank" rel="noopener">becoming a sponsor</a>.
  </WelcomeItem>
</template>

components/WelComeItem.vue

子组件 WelComeItem,看注释就好,代码可以先不看

<!-- 这个组件没有 script 代码 -->
<template>
  <div class="item">
    <i>
      <slot name="icon"></slot>
    </i>
    <div class="details">
      <h3>
        <slot name="heading"></slot>
      </h3>
      <slot></slot>
    </div>
  </div>
</template>

<style scoped>
.item {
  margin-top: 2rem;
  display: flex;
}

.details {
  flex: 1;
  margin-left: 1rem;
}

i {
  display: flex;
  place-items: center;
  place-content: center;
  width: 32px;
  height: 32px;

  color: var(--color-text);
}

h3 {
  font-size: 1.2rem;
  font-weight: 500;
  margin-bottom: 0.4rem;
  color: var(--color-heading);
}

@media (min-width: 1024px) {
  .item {
    margin-top: 0;
    padding: 0.4rem 0 1rem calc(var(--section-gap) / 2);
  }

  i {
    top: calc(50% - 25px);
    left: -26px;
    position: absolute;
    border: 1px solid var(--color-border);
    background: var(--color-background);
    border-radius: 8px;
    width: 50px;
    height: 50px;
  }

  .item:before {
    content: ' ';
    border-left: 1px solid var(--color-border);
    position: absolute;
    left: 0;
    bottom: calc(50% + 25px);
    height: calc(50% - 25px);
  }

  .item:after {
    content: ' ';
    border-left: 1px solid var(--color-border);
    position: absolute;
    left: 0;
    top: calc(50% + 25px);
    height: calc(50% - 25px);
  }

  .item:first-of-type:before {
    display: none;
  }

  .item:last-of-type:after {
    display: none;
  }
}
</style>

其他资源

其他文件就不一一细说了

小结

总结一下,学习 demo 项目的路径是:

  1. index.html
  2. src/main.js
  3. src/App.vue
  4. components/*.vue
  5. components/icons/*.vue
  6. 其他

相关文章

网友评论

      本文标题:【Vue3+Vite3】使用 Vite3 构建 demo 项目

      本文链接:https://www.haomeiwen.com/subject/mifgkdtx.html