给你整理了用 npm 和 cnpm 安装 Vue(以 Vue 3 为例)的步骤和注意事项:
1. 使用 npm 安装 Vue
- 初始化项目(如果还没初始化的话):
npm init -y
- 安装 Vue:
npm install vue@next
- 安装完成后,在项目中引入即可使用 Vue 3。
2. 使用 cnpm 安装 Vue
前提:cnpm 是淘宝团队维护的 npm 镜像客户端,速度更快,适合国内用户。
- 安装 cnpm(如果没安装):
npm install -g cnpm --registry=https://registry.npmmirror.com
- 初始化项目(如果还没初始化的话):
cnpm init -y
- 安装 Vue:
cnpm install vue@next
3. 注意事项
- Vue 3 版本 npm 包名是
vue@next
,Vue 2 用vue
。 - 使用 cnpm 安装时,registry 指向淘宝镜像源,速度更快。
- 安装完成后,记得用正确的方式在项目里引入 Vue。
好嘞!给你写一个从零开始用 Vite 创建 Vue 3 项目 的完整流程(包含 npm 和 cnpm 安装)以及项目启动步骤。
用 Vite 快速创建 Vue 3 项目(npm 和 cnpm 版)
1. 全局安装 create-vite
(可选,推荐用 npx/cnpmx)
npm install -g create-vite
# 或者
cnpm install -g create-vite
2. 创建新项目
用 npx(npm 方式):
npx create-vite@latest my-vue-app -- --template vue
用 cnpmx(cnpm 方式):
cnpmx create-vite@latest my-vue-app -- --template vue
这里 my-vue-app
是项目文件夹名,可以换成你想要的名字。
3. 进入项目目录
cd my-vue-app
4. 安装依赖
npm:
npm install
cnpm:
cnpm install
5. 启动开发服务器
npm run dev
# 或者
cnpm run dev
6. 打开浏览器访问
控制台会显示本地开发服务器地址,通常是:
http://localhost:5173
打开即可看到 Vue 3 项目启动页面。
额外说明
- Vite 是现代前端构建工具,支持极速热更新,非常适合 Vue 3 开发。
--template vue
指定了使用 Vue 模板。- 如果你想用 Vue 2,需要用其他脚手架(比如 vue-cli)。
- 使用 cnpm 主要是为了加速安装依赖,特别是国内网络环境。
好的!下面给你写一个典型 Vue 3 + Vite 项目结构解析和如何新增组件、添加 Vue Router 和 Pinia(状态管理)的示范。
一、Vite + Vue3 项目目录结构示例
my-vue-app/
├── index.html # 入口 HTML
├── package.json # npm 配置文件
├── vite.config.js # Vite 配置文件
├── node_modules/ # 依赖包
├── public/ # 静态资源
└── src/
├── main.js # 入口 JS,挂载 Vue
├── App.vue # 根组件
├── assets/ # 图片、样式等资源
├── components/ # 组件目录
│ └── HelloWorld.vue
├── router/ # 路由相关
│ └── index.js
└── stores/ # Pinia 状态管理
└── index.js
二、添加新组件示例
在 src/components
目录下创建 MyComponent.vue
:
<template>
<div>
<h2>这是一个新组件</h2>
</div>
</template>
<script setup>
// 这里写组件逻辑
</script>
<style scoped>
h2 {
color: #42b983;
}
</style>
然后在 App.vue
或其他组件里引入:
<script setup>
import MyComponent from './components/MyComponent.vue'
</script>
<template>
<div>
<MyComponent />
</div>
</template>
三、添加 Vue Router
- 安装:
npm install vue-router@4
# 或 cnpm install vue-router@4
- 创建路由配置
src/router/index.js
:
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../components/Home.vue'
import About from '../components/About.vue'
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
const router = createRouter({
history: createWebHistory(),
routes,
})
export default router
- 在
src/main.js
中挂载路由:
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')
- 使用
<router-view />
显示路由组件:
修改 App.vue
:
<template>
<router-view></router-view>
</template>
四、添加 Pinia 状态管理
- 安装:
npm install pinia
# 或 cnpm install pinia
- 创建状态管理
src/stores/index.js
:
import { defineStore } from 'pinia'
export const useMainStore = defineStore('main', {
state: () => ({
count: 0
}),
actions: {
increment() {
this.count++
}
}
})
- 在
main.js
挂载 Pinia:
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
const app = createApp(App)
app.use(router)
app.use(createPinia())
app.mount('#app')
- 组件中使用状态:
<script setup>
import { useMainStore } from './stores'
const store = useMainStore()
</script>
<template>
<div>
<p>计数:{{ store.count }}</p>
<button @click="store.increment">增加</button>
</div>
</template>
发表回复