feat: add Vue2 exercises for dynamic styles, lifecycle methods, component communication, and course management dashboard

- Implement dynamic styles and event handling in Vue2 with a card component.
- Create lifecycle methods exercise to simulate async data loading and instance destruction.
- Develop a component communication exercise with props, events, and slots.
- Build a comprehensive course management dashboard with filtering, statistics, and component interactions.
This commit is contained in:
charlie
2026-03-23 10:09:29 +08:00
parent 00d3c9e4c6
commit 3435848495
48 changed files with 1705 additions and 48 deletions

View File

@@ -0,0 +1,33 @@
# 练习 8组件通信与综合练习
## 目标
把 Vue2 最核心的组件能力串起来,完成一个小型页面。
## 你要练什么
- 组件注册(全局 / 局部)
- `props`
- `props` 校验
- `$emit`
- `slot`
- `ref`
## 任务
请基于页面结构完成以下操作:
- 把课程卡片拆成子组件
- 注册一个全局角标组件
- 父组件通过 `props` 传课程数据
-`props` 补上类型、必填、默认值或校验规则
- 子组件点击按钮后通过 `$emit` 通知父组件切换状态
-`slot` 自定义按钮文案
-`ref` 聚焦输入框
## 文件
- [starter.html](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/08-components-communication-and-final/starter.html)
- [starter.js](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/08-components-communication-and-final/starter.js)
- [answer.html](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/08-components-communication-and-final/answer.html)
- [answer.js](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/08-components-communication-and-final/answer.js)

View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>组件通信与综合练习</title>
<style>
body { margin: 0; padding: 32px; font-family: "PingFang SC", sans-serif; background: #f4f7fb; }
.wrap { max-width: 860px; margin: 0 auto; display: grid; gap: 16px; }
.toolbar, .card { padding: 20px; border-radius: 18px; background: #fff; border: 1px solid #d9e3f2; }
.list { display: grid; gap: 14px; }
button { padding: 10px 14px; border: 0; border-radius: 999px; background: #2d6cdf; color: #fff; cursor: pointer; }
input { width: 100%; padding: 12px; border-radius: 12px; border: 1px solid #cfd8e8; }
.done { color: #1f8f54; }
</style>
</head>
<body>
<section id="app" class="wrap">
<article class="toolbar">
<h1>Vue2 综合练习</h1>
<input ref="keywordInput" v-model="keyword" type="text" placeholder="输入关键字过滤课程" />
<button type="button" @click="focusInput">聚焦输入框</button>
</article>
<section class="list">
<course-card
v-for="course in filteredCourses"
:key="course.id"
:course="course"
theme="accent"
@toggle="toggleCourse"
>
<template v-slot:action>
切换完成状态
</template>
</course-card>
</section>
</section>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="./answer.js"></script>
</body>
</html>

View File

@@ -0,0 +1,80 @@
Vue.component("course-badge", {
props: {
label: {
type: String,
default: "默认角标",
},
},
template: `<span style="display:inline-block;margin-bottom:8px;padding:4px 10px;border-radius:999px;background:#eef4ff;color:#2d6cdf;">{{ label }}</span>`,
});
const CourseCard = {
props: {
course: {
type: Object,
required: true,
},
theme: {
type: String,
default: "normal",
validator(value) {
return ["normal", "accent"].includes(value);
},
},
},
template: `
<article class="card" :style="theme === 'accent' ? 'border-color:#2d6cdf;' : ''">
<course-badge :label="course.level"></course-badge>
<h2>{{ course.title }}</h2>
<p :class="{ done: course.finished }">
{{ course.finished ? "已完成" : "学习中" }}
</p>
<button type="button" @click="$emit('toggle', course.id)">
<slot name="action">操作</slot>
</button>
</article>
`,
};
new Vue({
el: "#app",
components: {
CourseCard,
},
data: {
keyword: "",
courses: [
{ id: 1, title: "Vue 实例", finished: true, level: "基础" },
{ id: 2, title: "模板语法", finished: false, level: "基础" },
{ id: 3, title: "组件通信", finished: false, level: "进阶" },
],
},
computed: {
filteredCourses() {
const value = this.keyword.trim().toLowerCase();
if (!value) {
return this.courses;
}
return this.courses.filter((course) => course.title.toLowerCase().includes(value));
},
},
methods: {
toggleCourse(courseId) {
this.courses = this.courses.map((course) => {
if (course.id === courseId) {
return {
...course,
finished: !course.finished,
};
}
return course;
});
},
focusInput() {
this.$refs.keywordInput.focus();
},
},
});

View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>组件通信与综合练习</title>
<style>
body { margin: 0; padding: 32px; font-family: "PingFang SC", sans-serif; background: #f4f7fb; }
.wrap { max-width: 860px; margin: 0 auto; display: grid; gap: 16px; }
.toolbar, .card { padding: 20px; border-radius: 18px; background: #fff; border: 1px solid #d9e3f2; }
.list { display: grid; gap: 14px; }
button { padding: 10px 14px; border: 0; border-radius: 999px; background: #2d6cdf; color: #fff; cursor: pointer; }
input { width: 100%; padding: 12px; border-radius: 12px; border: 1px solid #cfd8e8; }
.done { color: #1f8f54; }
</style>
</head>
<body>
<section id="app" class="wrap">
<article class="toolbar">
<h1>Vue2 综合练习</h1>
<input ref="keywordInput" v-model="keyword" type="text" placeholder="输入关键字过滤课程" />
<button type="button" @click="focusInput">聚焦输入框</button>
</article>
<section class="list">
<course-card
v-for="course in filteredCourses"
:key="course.id"
:course="course"
theme="accent"
@toggle="toggleCourse"
>
<template v-slot:action>
切换完成状态
</template>
</course-card>
</section>
</section>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="./starter.js"></script>
</body>
</html>

View File

@@ -0,0 +1,66 @@
Vue.component("course-badge", {
props: {
label: {
type: String,
default: "默认角标",
},
},
template: `<span style="display:inline-block;margin-bottom:8px;padding:4px 10px;border-radius:999px;background:#eef4ff;color:#2d6cdf;">{{ label }}</span>`,
});
const CourseCard = {
props: {
course: {
type: Object,
required: true,
},
theme: {
type: String,
default: "normal",
validator(value) {
return ["normal", "accent"].includes(value);
},
},
},
template: `
<article class="card" :style="theme === 'accent' ? 'border-color:#2d6cdf;' : ''">
<course-badge :label="course.level"></course-badge>
<h2>{{ course.title }}</h2>
<p :class="{ done: course.finished }">
{{ course.finished ? "已完成" : "学习中" }}
</p>
<button type="button" @click="$emit('toggle', course.id)">
<slot name="action">操作</slot>
</button>
</article>
`,
};
new Vue({
el: "#app",
components: {
CourseCard,
},
data: {
keyword: "",
courses: [
{ id: 1, title: "Vue 实例", finished: true, level: "基础" },
{ id: 2, title: "模板语法", finished: false, level: "基础" },
{ id: 3, title: "组件通信", finished: false, level: "进阶" },
],
},
computed: {
filteredCourses() {
// 任务:根据 keyword 过滤课程列表
return this.courses;
},
},
methods: {
toggleCourse(courseId) {
// 任务:根据 courseId 切换 finished
},
focusInput() {
// 任务:通过 ref 聚焦输入框
},
},
});