Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

使用vite创建vue3项目

作者:任光阴风干ゝ影象   发布日期:2025-06-12   浏览:50

以下是包含代码的示例,以符合您的要求:

使用 Vite 创建 Vue 3 项目

1. 安装 Vite 和 创建项目

首先,确保你已经安装了 Node.js。然后使用以下命令创建一个新的 Vue 3 项目:

npm create vite@latest my-vue-app --template vue
cd my-vue-app
npm install

2. 启动开发服务器

安装完成后,你可以启动开发服务器:

npm run dev

3. 项目结构

Vite 生成的 Vue 3 项目结构如下:

my-vue-app/
├── node_modules/
├── public/
│   └── vite.svg
├── src/
│   ├── assets/
│   │   └── logo.png
│   ├── components/
│   │   └── HelloWorld.vue
│   ├── App.vue
│   └── main.js
├── .gitignore
├── index.html
├── package.json
├── README.md
└── vite.config.js

4. 示例代码

src/main.js 是项目的入口文件:

import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

src/App.vue 是主组件:

<template>
  <img alt="Vue logo" src="./assets/logo.png">
  <HelloWorld msg="Welcome to Your Vue.js + Vite App"/>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

src/components/HelloWorld.vue 是一个简单的组件:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

<style scoped>
h1 {
  color: #42b983;
}
</style>

5. 配置 Vite

vite.config.js 是 Vite 的配置文件,默认情况下不需要修改:

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

export default defineConfig({
  plugins: [vue()]
})

以上就是使用 Vite 创建 Vue 3 项目的完整步骤和示例代码。希望对你有所帮助!

上一篇:vue2 render

下一篇:vue3 子组件调用父组件的方法

大家都在看

vue.config.js configu

node.js vue

vue查看版本

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

vue 遍历list

typescript vue

vue 复选框

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站