Vue3是一个渐进式JavaScript框架,用于构建用户界面。它由尤雨溪(Evan You)创建,于2020年9月发布。Vue3在性能、TypeScript支持和组合式API方面进行了重大改进。
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">{{ message }}</div>
<script>
const { createApp } = Vue;
createApp({
data() {
return {
message: 'Hello Vue3!'
}
}
}).mount('#app');
</script>
</body>
</html>
# 使用npm
npm create vue@latest
# 使用yarn
yarn create vue
# 使用pnpm
pnpm create vue
按照提示选择配置:
✔ Project name: my-vue-app
✔ Add TypeScript? No / Yes
✔ Add JSX Support? No / Yes
✔ Add Vue Router? No / Yes
✔ Add Pinia? No / Yes
✔ Add Vitest? No / Yes
✔ Add ESLint? No / Yes
cd my-vue-app
npm install
npm run dev
创建 index.html:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的第一个Vue3应用</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<h1>{{ title }}</h1>
<p>{{ description }}</p>
<button @click="count++">点击次数: {{ count }}</button>
</div>
<script>
const { createApp } = Vue;
createApp({
data() {
return {
title: '欢迎学习Vue3',
description: 'Vue3是一个渐进式JavaScript框架',
count: 0
}
}
}).mount('#app');
</script>
</body>
</html>
my-vue-app/
├── node_modules/ # 依赖包
├── public/ # 静态资源
├── src/
│ ├── assets/ # 资源文件
│ ├── components/ # 组件
│ ├── App.vue # 根组件
│ └── main.js # 入口文件
├── index.html # HTML模板
├── package.json # 项目配置
└── vite.config.js # Vite配置
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')