Vue的快速入门

Vue 简介

Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心特点是轻量级、易上手,同时支持组件化开发和响应式数据绑定。

安装与基础配置

确保已安装 Node.js(建议版本 16+),通过以下命令创建 Vue 项目:

bash 复制代码
npm init vue@latest
cd your-project
npm install
npm run dev

核心概念与示例

响应式数据绑定

Vue 使用 refreactive 声明响应式数据。以下是一个计数器示例:

html 复制代码
<template>
  <button @click="count++">Count: {{ count }}</button>
</template>

<script setup>
import { ref } from 'vue';
const count = ref(0);
</script>
组件化开发

组件是 Vue 的核心功能。创建一个子组件 ChildComponent.vue

html 复制代码
<template>
  <p>{{ message }}</p>
</template>

<script setup>
defineProps(['message']);
</script>

在父组件中使用:

html 复制代码
<template>
  <ChildComponent message="Hello Vue!" />
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';
</script>
条件与循环

Vue 通过 v-ifv-for 实现条件渲染和列表渲染:

html 复制代码
<template>
  <div v-if="show">显示内容</div>
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.name }}</li>
  </ul>
</template>

<script setup>
import { ref } from 'vue';
const show = ref(true);
const items = ref([{ id: 1, name: 'Vue' }, { id: 2, name: 'React' }]);
</script>
事件处理

通过 @clickv-on 绑定事件:

html 复制代码
<template>
  <button @click="greet">点击</button>
</template>

<script setup>
function greet() {
  alert('Hello Vue!');
}
</script>

进阶功能

状态管理(Pinia)

安装 Pinia:

bash 复制代码
npm install pinia

创建 store:

javascript 复制代码
// stores/counter.js
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++;
    }
  }
});

在组件中使用:

html 复制代码
<template>
  <button @click="counter.increment">Count: {{ counter.count }}</button>
</template>

<script setup>
import { useCounterStore } from '@/stores/counter';
const counter = useCounterStore();
</script>
路由(Vue Router)

安装 Vue Router:

bash 复制代码
npm install vue-router

配置路由:

javascript 复制代码
// router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
const routes = [{ path: '/', component: Home }];
const router = createRouter({ history: createWebHistory(), routes });
export default router;

main.js 中引入:

javascript 复制代码
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
createApp(App).use(router).mount('#app');

调试与优化

  • 浏览器工具:安装 Vue Devtools 扩展,便于调试组件和状态。
  • 性能优化 :使用 v-oncev-memo 减少不必要的渲染。

学习资源

  • 官方文档:vuejs.org
  • 实战项目:参考 GitHub 上的 Vue 模板仓库(如 vuejs/vue-cli)。

通过以上步骤和示例,可以快速掌握 Vue 的核心功能并投入开发。

相关推荐
Cult Of21 小时前
Alicea Wind的个人网站开发日志(2)
开发语言·python·vue
Byron07072 天前
从多端割裂到体验统一:基于 Vue 生态的跨端架构落地实战
vue·多端
计算机程序设计小李同学2 天前
基于 Spring Boot + Vue 的龙虾专营店管理系统的设计与实现
java·spring boot·后端·spring·vue
沐墨染2 天前
Vue实战:自动化研判报告组件的设计与实现
前端·javascript·信息可视化·数据分析·自动化·vue
奔跑的呱呱牛2 天前
viewer-utils 图片预览工具库
javascript·vue·react
Cult Of2 天前
Alicea Wind的个人网站开发日志(1)
python·vue
Polaris_YJH2 天前
使用Vue3+Vite+Pinia+elementUI搭建初级企业级项目
前端·javascript·elementui·vue
Mr Xu_2 天前
【Vue3 + ECharts 实战】正确使用 showLoading、resize 与 dispose 避免内存泄漏
前端·信息可视化·vue·echarts
换日线°3 天前
前端炫酷展开效果
前端·javascript·vue
IT北辰3 天前
基于Vue3+python+mysql8.0的财务凭证录入系统,前后端分离完整版(可赠送源码)
python·vue