当前位置: 首页 > news >正文

vue3 基本教程-运行一个最小demo

Vue 3 基本教程 - 运行一个最小 Demo

1. 创建项目

使用 Vue 官方脚手架工具创建一个新项目:

# 安装 Vue CLI (如果尚未安装)
npm install -g @vue/cli# 创建一个新项目
vue create vue3-demo# 选择 Vue 3 预设
# 使用方向键选择 "Default (Vue 3)" 然后按 Enter

或者使用 Vite(更快的开发工具):

# 使用 Vite 创建项目
npm create vite@latest vue3-demo -- --template vue# 进入项目目录
cd vue3-demo

2. 项目结构

基本项目结构如下:

vue3-demo/
├── node_modules/
├── public/
├── src/
│   ├── assets/
│   ├── components/
│   ├── App.vue         # 主组件
│   └── main.js         # 入口文件
├── package.json
└── vite.config.js (或 vue.config.js)

3. 修改 App.vue

打开 src/App.vue 文件,将其修改为一个简单的计数器应用:

<template><div class="container"><h1>Vue 3 最小 Demo</h1><p>当前计数: {{ count }}</p><button @click="increment">增加</button><button @click="decrement">减少</button></div>
</template><script setup>
import { ref } from 'vue'// 使用 ref 创建响应式状态
const count = ref(0)// 方法
function increment() {count.value++
}function decrement() {count.value--
}
</script><style scoped>
.container {max-width: 400px;margin: 0 auto;padding: 20px;text-align: center;font-family: Arial, sans-serif;
}button {margin: 0 5px;padding: 8px 16px;background-color: #4CAF50;color: white;border: none;border-radius: 4px;cursor: pointer;
}button:hover {background-color: #45a049;
}
</style>

4. 运行项目

安装依赖并启动开发服务器:

# 安装依赖
npm install# 启动开发服务器
npm run dev

启动后,在浏览器中打开显示的地址(通常是 http://localhost:5173 或 http://localhost:8080)

5. 代码解释

  • script setup: Vue 3 的 Composition API 语法糖,简化了组件的编写
  • ref(0): 创建一个值为 0 的响应式引用,当它变化时会自动更新视图
  • count.value: 访问或修改 ref 的值需要使用 .value 属性
  • @click: Vue 的事件绑定语法,点击按钮时调用对应的方法
  • {{ count }}: 模板中的插值表达式,显示响应式变量的值

6. 添加一个新组件

创建 src/components/HelloWorld.vue 文件:

<template><div class="hello"><h2>{{ msg }}</h2></div>
</template><script setup>
defineProps({msg: {type: String,required: true}
})
</script><style scoped>
.hello {background-color: #f5f5f5;padding: 10px;border-radius: 5px;margin-top: 20px;
}
</style>

修改 App.vue 以使用新组件:

<template><div class="container"><h1>Vue 3 最小 Demo</h1><p>当前计数: {{ count }}</p><button @click="increment">增加</button><button @click="decrement">减少</button><HelloWorld msg="这是一个子组件" /></div>
</template><script setup>
import { ref } from 'vue'
import HelloWorld from './components/HelloWorld.vue'// 使用 ref 创建响应式状态
const count = ref(0)// 方法
function increment() {count.value++
}function decrement() {count.value--
}
</script><!-- 样式部分保持不变 -->

7. 总结

这个最小 Demo 展示了 Vue 3 的基本功能:

  • 响应式状态管理(使用 ref)
  • 事件处理
  • 组件创建和使用
  • prop 传递

Vue 3 的 Composition API 和 script setup 语法使代码更简洁、更易于组织和重用。


文章转载自:

http://PN6ehVrR.nqgff.cn
http://PY4mHG2Q.nqgff.cn
http://1V2L8CTg.nqgff.cn
http://CcFcPi2I.nqgff.cn
http://oy0iEG3N.nqgff.cn
http://5Fr6ewET.nqgff.cn
http://KIWmTCL5.nqgff.cn
http://00F3P6O2.nqgff.cn
http://0V4GdNb2.nqgff.cn
http://0ValMoZO.nqgff.cn
http://GmBL3wAn.nqgff.cn
http://vZe5LKjM.nqgff.cn
http://HbgvMjwD.nqgff.cn
http://AWAiXtv5.nqgff.cn
http://T5VFGsUL.nqgff.cn
http://A688nVqY.nqgff.cn
http://fbpulkKS.nqgff.cn
http://SPMnDTG7.nqgff.cn
http://6S2h4wYZ.nqgff.cn
http://nPEEW0PL.nqgff.cn
http://7z9yfZif.nqgff.cn
http://rG1u2wsF.nqgff.cn
http://X10ltbxO.nqgff.cn
http://L86FyiMK.nqgff.cn
http://jw9fc5wP.nqgff.cn
http://1LBTyIRy.nqgff.cn
http://UH9M2TjQ.nqgff.cn
http://wj7BT4Ra.nqgff.cn
http://kr9cK4Ti.nqgff.cn
http://EfCGAABZ.nqgff.cn
http://www.dtcms.com/a/384203.html

相关文章:

  • [JavaWeb]在学习Servlet的过程中一个经典面试题
  • 安全测试技能 | web、app、PC应用测试面试题梳理
  • 金融数据--集思录可转债等权指数
  • ruoyi分布式在module下新建服务排坑指南
  • prometheus-2.42.0.linux-amd64.tar.gz 安装配置展示
  • 1台SolidWorks服务器能带8-10人并发使用
  • 中国制造难点在哪里?
  • 网编_HW_9.15
  • 前端基础知识---10 Node.js(一)
  • C语言:求三个整数中的最大值
  • AI 赋能大前端电商应用:智能尺码推荐与搭配建议,重构购物体验
  • 跨境通信合规新解:Z世代多模态交互技术突破
  • SpringBoot返回前端时间格式化处理
  • 高系分四:网络分布式
  • Python 3.9.21 升级到 Python >=3.10
  • 在运维工作中,FTP主动和被动的区别有哪些?
  • CE-Agent 多智能体系统流程图文档
  • 数据结构——逻辑结构物理结构
  • RuoYi-Vue3-FastAPI框架的功能实现(下)
  • PySpark简化数据处理的高效函数有哪些?
  • 哈尔滨云前沿服务器租用托管
  • React项目 新闻发布系统 项目初始化与路由搭建
  • 数字经济专业核心课程解析与职业发展指南
  • Spring Boot 全栈优化:服务器、数据、缓存、日志的场景应用!
  • 三色标记算法
  • Apache IoTDB(5):深度解析时序数据库 IoTDB 在 AINode 模式单机和集群的部署与实践
  • 【Java后端】Spring Security配置对应的账号密码访问
  • 精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
  • 《Elasticsearch全文检索核心技术解析》
  • Rocky Linux10.0修改ip地址