Vue3 学习教程,从入门到精通,Vue 3 + Tailwind CSS 全面知识点与案例详解(31)
Vue 3 + Tailwind CSS 全面知识点与案例详解
一、Vue 3 核心语法知识点
1. Vue 3 基础
- 创建 Vue 3 项目
使用 Vite 创建项目:npm create vue@latest # 选择需要的特性(如 TypeScript、Vue Router)
- 响应式数据
使用ref
和reactive
:import { ref, reactive } from 'vue'; const count = ref(0); // 响应式变量 const user = reactive({ name: 'Alice', age: 25 }); // 响应式对象
- 生命周期钩子
import { onMounted, onUpdated } from 'vue'; onMounted(() => {console.log('组件挂载完成'); }); onUpdated(() => {console.log('组件更新完成'); });
- 组件化开发
创建组件components/HelloWorld.vue
:<template><div class="text-2xl font-bold text-blue-500">Hello Vue 3!</div> </template>
- 路由(Vue Router)
安装并配置路由:npm install vue-router@4
router/index.js
:import { createRouter, createWebHistory } from 'vue-router'; const routes = [{ path: '/', component: () => import('../views/Home.vue') },{ path: '/about', component: () => import('../views/About.vue') } ]; export default createRouter({ history: createWebHistory(), routes });
2. Tailwind CSS 集成
- 安装依赖
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p # 生成 tailwind.config.js 和 postcss.config.js
- 配置 Tailwind
tailwind.config.js
:module.exports = {content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],theme: {extend: {colors: { 'brand': '#3490dc' }, // 自定义颜色spacing: { '128': '32rem' }, // 自定义间距},},plugins: [], };
- 引入 Tailwind 样式
src/index.css
:@tailwind base; @tailwind components; @tailwind utilities;
main.js
:import './index.css';
二、Tailwind CSS 核心语法知识点
1. 实用工具类
- 布局类
<div class="flex justify-center items-center h-screen">居中容器</div> <div class="grid grid-cols-3 gap-4">三列网格布局</div>
- 颜色与背景
<div class="bg-blue-500 text-white p-4">蓝色背景白字</div> <div class="bg-gradient-to-r from-red-500 to-yellow-500">渐变背景</div>
- 文本样式
<h1 class="text-4xl font-bold underline">大标题</h1> <p class="text-gray-700 text-lg">灰色段落</p>
- 边框与阴影
<div class="border border-gray-300 rounded-lg shadow-md p-4">带边框和阴影的卡片</div>
- 交互效果
<button class="bg-blue-500 hover:bg-blue-700 text-white py-2 px-4 rounded">悬停按钮 </button>
- 响应式设计
<div class="block sm:hidden md:block">在小屏隐藏,中屏显示</div>
2. 自定义配置
- 扩展主题
tailwind.config.js
:theme: {extend: {fontFamily: { sans: ['Inter', 'sans-serif'] }, // 添加字体animation: { spin: 'spin 2s linear infinite' }, // 自定义动画}, },
- 插件
安装插件(如@tailwindcss/forms
):npm install -D @tailwindcss/forms
tailwind.config.js
:plugins: [require('@tailwindcss/forms')],
三、综合案例:Vue 3 + Tailwind CSS 仪表盘页面
1. 案例目标
- 创建一个响应式仪表盘页面,包含导航栏、卡片组件、按钮交互和动画效果。
2. 代码实现
src/views/Dashboard.vue
<template><div class="min-h-screen bg-gray-100"><!-- 导航栏 --><nav class="bg-white shadow-md p-4 flex justify-between items-center"><h1 class="text-2xl font-bold text-blue-600">仪表盘</h1><div class="space-x-4"><button class="bg-blue-500 hover:bg-blue-700 text-white py-2 px-4 rounded">首页</button><button class="bg-gray-300 hover:bg-gray-400 text-gray-800 py-2 px-4 rounded">用户</button></div></nav><!-- 主体内容 --><main class="p-6 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"><!-- 卡片组件 --><div v-for="card in cards" :key="card.title" class="bg-white rounded-lg shadow p-4"><h2 class="text-xl font-semibold mb-2">{{ card.title }}</h2><p class="text-gray-600">{{ card.description }}</p><!-- 动态类绑定 --><div :class="`mt-4 text-sm ${card.status === '活跃' ? 'text-green-500' : 'text-red-500'}`">状态: {{ card.status }}</div></div></main><!-- 动画按钮 --><div class="flex justify-center mt-8"><button @click="animateButton" :class="`bg-purple-500 hover:bg-purple-600 text-white py-3 px-6 rounded transition-transform duration-300 ${isAnimating ? 'scale-110' : 'scale-100'}`">点击动画</button></div></div>
</template><script setup>
import { ref } from 'vue';// 响应式数据
const cards = ref([{ title: '用户统计', description: '展示用户增长趋势', status: '活跃' },{ title: '销售额', description: '本月销售额分析', status: '活跃' },{ title: '错误日志', description: '最近的系统错误', status: '停用' },
]);const isAnimating = ref(false);// 动画触发
const animateButton = () => {isAnimating.value = true;setTimeout(() => {isAnimating.value = false;}, 300);
};
</script>
3. 代码注释说明
- 导航栏:使用
flex
布局实现响应式导航栏,结合shadow-md
添加阴影效果。 - 卡片组件:通过
grid
布局实现响应式多列卡片,使用v-for
动态渲染。 - 动态类绑定:根据卡片状态动态切换文字颜色(绿色/红色)。
- 动画按钮:通过
ref
控制按钮的scale
变化,实现点击缩放动画。
四、常见问题与解决方案
- Tailwind 样式未生效
- 检查
tailwind.config.js
的content
路径是否正确。 - 确保
index.css
已正确引入到main.js
。
- 检查
- 响应式设计失效
- 检查是否遗漏
viewport
meta 标签:<meta name="viewport" content="width=device-width, initial-scale=1.0">
- 检查是否遗漏
- 动画性能问题
- 使用
transition
和duration
控制动画流畅度,避免过度渲染。
- 使用
五、总结
通过本案例,你掌握了:
- Vue 3 的核心语法(响应式数据、组件化、路由)。
- Tailwind CSS 的实用工具类和自定义配置。
- 如何结合 Vue 3 和 Tailwind CSS 构建响应式、交互式界面。
下一步建议:
- 学习 Vue 3 的 Composition API 和 Pinia 状态管理。
- 探索 Tailwind CSS 的插件生态(如
@tailwindcss/typography
)。 - 实践更复杂的项目(如管理系统、电商网站)。