项目启动:搭建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>

页面展示

相关推荐
陈随易7 小时前
VSCode的Copilot扩展支持接入DeepSeek,Kimi了!
前端·后端·程序员
我不是外星人8 小时前
有了 Harness Engineering ,真的还需要研发工程师吗?
前端·后端·ai编程
candyTong8 小时前
RTK 技术原理:一次典型会话里,80% 上下文是怎么省下来的
javascript·后端·架构
IT_陈寒11 小时前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
Jackson__12 小时前
分享一个横向滚动案例,带悬停暂停,通用性很强
前端
MariaH12 小时前
git rebase的使用
前端
_柳青杨12 小时前
深入理解 JavaScript 事件循环
前端·javascript
阡陌Jony12 小时前
关于前端性能优化的一些问题:
前端
用户6000718191013 小时前
【翻译】简化 TSRX
前端
IT乐手14 小时前
佛德角逼平西班牙,国足还有啥借口?
前端