以下是包含代码的示例,以符合您的要求:
首先,确保你已经安装了 Node.js。然后使用以下命令创建一个新的 Vue 3 项目:
npm create vite@latest my-vue-app --template vue
cd my-vue-app
npm install
安装完成后,你可以启动开发服务器:
npm run dev
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
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>
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 子组件调用父组件的方法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站