项目启动:搭建Vue 3工程化项目

init 项目

perl 复制代码
nvm install 20
nvm use 20
npm create vite

npm i vue-router@^4.3.0 vuex@^4.1.0

项目结构

arduino 复制代码
├── README.md

├── index.html 入口文件

├── package.json

├── public 资源文件

│ └── favicon.ico

├── src 源码

│ ├── App.vue 单文件组件

│ ├── assets

│ │ └── logo.png

│ ├── components

│ │ └── HelloWorld.vue

│ └── main.js 入口

└── vite.config.js vite工程化配置文件

src 结构规范化

css 复制代码
├── src

│ ├── api 数据请求

│ ├── assets 静态资源

│ ├── components 组件

│ ├── pages 页面

│ ├── router 路由配置

│ ├── store vuex数据

│ └── utils 工具函数

添加一些文件

vbnet 复制代码
router/index.js

import {
createRouter,
createWebHashHistory,
} from 'vue-router'
import Home from '../pages/home.vue'
import About from '../pages/about.vue'

const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
}
]
const router = createRouter({
history: createWebHashHistory(),
routes
})
export default router
xml 复制代码
pages/about.vue  

<template>
<h1>这是关于页面</h1>
</template>
xml 复制代码
pages/home.vue  

<template>
<h1>这是首页页面</h1>
</template>
xml 复制代码
App.vue

<script setup>
</script>

<template>
  <div class="container">
    <header>
      <h1>我的 Vue3 应用</h1>
      <nav>
        <router-link to="/">首页</router-link> |
        <router-link to="/about">关于</router-link>
      </nav>
    </header>

    <main>
      <router-view />
    </main>
  </div>
</template>

<style scoped>
.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

header {
  text-align: center;
  margin-bottom: 20px;
}

nav {
  margin-top: 10px;
}

main {
  min-height: 300px;
}

footer {
  margin-top: 40px;
  text-align: center;
  color: #888;
}

.logo {
  height: 6em;
  padding: 1.5em;
  will-change: filter;
  transition: filter 300ms;
}
.logo:hover {
  filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
  filter: drop-shadow(0 0 2em #42b883aa);
}
</style>

页面展示

相关推荐
几何心凉4 小时前
openGauss:多核时代企业级数据库的性能与高可用新标杆
前端·数据库·数据库开发
AiXed6 小时前
PC微信协议之AES-192-GCM算法
前端·数据库·python
AllData公司负责人6 小时前
实时开发平台(Streampark)--Flink SQL功能演示
大数据·前端·架构·flink·开源
小满zs6 小时前
Next.js第五章(动态路由)
前端
清沫6 小时前
VSCode debugger 调试指南
前端·javascript·visual studio code
一颗宁檬不酸7 小时前
页面布局练习
前端·html·页面布局
zhenryx7 小时前
React Native 自定义 ScrollView 滚动条:开箱即用的 IndicatorScrollView(附源码示例)
javascript·react native·react.js·typescript
金木讲编程8 小时前
Claude、Agent与Copilot协作生成Angular应用
前端·ai编程
振华OPPO8 小时前
Vue:“onMounted“ is defined but never used no-unused-vars
前端·javascript·css·vue.js·前端框架
欧雷殿9 小时前
在富阳银湖成立地域化的软件研发团队
前端·程序员·创业