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:
31
07-vue2/07-lifecycle-and-async/README.md
Normal file
31
07-vue2/07-lifecycle-and-async/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# 练习 7:生命周期和异步更新
|
||||
|
||||
## 目标
|
||||
|
||||
学会在合适的生命周期里加载数据,并理解页面更新的时机。
|
||||
|
||||
## 你要练什么
|
||||
|
||||
- `created`
|
||||
- `mounted`
|
||||
- `updated`
|
||||
- `beforeDestroy`
|
||||
- `destroyed`
|
||||
- 模拟异步请求
|
||||
|
||||
## 任务
|
||||
|
||||
请基于页面结构完成以下操作:
|
||||
|
||||
- 进入页面时显示 loading
|
||||
- 在 `mounted` 里模拟异步获取课程数据
|
||||
- 数据返回后渲染课程列表
|
||||
- 在 `updated` 里输出更新日志
|
||||
- 点击按钮时销毁当前实例,并观察销毁日志
|
||||
|
||||
## 文件
|
||||
|
||||
- [starter.html](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/07-lifecycle-and-async/starter.html)
|
||||
- [starter.js](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/07-lifecycle-and-async/starter.js)
|
||||
- [answer.html](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/07-lifecycle-and-async/answer.html)
|
||||
- [answer.js](/Users/lijiaqing/home/wwwroot/front-end-example/07-vue2/07-lifecycle-and-async/answer.js)
|
||||
25
07-vue2/07-lifecycle-and-async/answer.html
Normal file
25
07-vue2/07-lifecycle-and-async/answer.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!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: #f6f8fb; }
|
||||
.panel { max-width: 760px; margin: 0 auto; padding: 24px; border-radius: 18px; background: #fff; border: 1px solid #dce4f1; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<section id="app" class="panel">
|
||||
<h1>生命周期练习</h1>
|
||||
<button type="button" @click="destroyInstance">销毁当前实例</button>
|
||||
<p v-if="loading">数据加载中...</p>
|
||||
<ul v-else>
|
||||
<li v-for="item in courses" :key="item.id">{{ item.title }}</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
|
||||
<script src="./answer.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
36
07-vue2/07-lifecycle-and-async/answer.js
Normal file
36
07-vue2/07-lifecycle-and-async/answer.js
Normal file
@@ -0,0 +1,36 @@
|
||||
new Vue({
|
||||
el: "#app",
|
||||
data: {
|
||||
loading: true,
|
||||
courses: [],
|
||||
},
|
||||
created() {
|
||||
console.log("created: 实例已经创建");
|
||||
},
|
||||
mounted() {
|
||||
console.log("mounted: 页面已经挂载");
|
||||
|
||||
setTimeout(() => {
|
||||
this.courses = [
|
||||
{ id: 1, title: "生命周期基础" },
|
||||
{ id: 2, title: "异步数据渲染" },
|
||||
{ id: 3, title: "组件更新时机" },
|
||||
];
|
||||
this.loading = false;
|
||||
}, 1000);
|
||||
},
|
||||
updated() {
|
||||
console.log("updated: 页面数据已经更新");
|
||||
},
|
||||
beforeDestroy() {
|
||||
console.log("beforeDestroy: 实例即将销毁");
|
||||
},
|
||||
destroyed() {
|
||||
console.log("destroyed: 实例已经销毁");
|
||||
},
|
||||
methods: {
|
||||
destroyInstance() {
|
||||
this.$destroy();
|
||||
},
|
||||
},
|
||||
});
|
||||
25
07-vue2/07-lifecycle-and-async/starter.html
Normal file
25
07-vue2/07-lifecycle-and-async/starter.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!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: #f6f8fb; }
|
||||
.panel { max-width: 760px; margin: 0 auto; padding: 24px; border-radius: 18px; background: #fff; border: 1px solid #dce4f1; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<section id="app" class="panel">
|
||||
<h1>生命周期练习</h1>
|
||||
<button type="button" @click="destroyInstance">销毁当前实例</button>
|
||||
<p v-if="loading">数据加载中...</p>
|
||||
<ul v-else>
|
||||
<li v-for="item in courses" :key="item.id">{{ item.title }}</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
|
||||
<script src="./starter.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
30
07-vue2/07-lifecycle-and-async/starter.js
Normal file
30
07-vue2/07-lifecycle-and-async/starter.js
Normal file
@@ -0,0 +1,30 @@
|
||||
new Vue({
|
||||
el: "#app",
|
||||
data: {
|
||||
loading: true,
|
||||
courses: [],
|
||||
},
|
||||
created() {
|
||||
console.log("created: 实例已经创建");
|
||||
},
|
||||
mounted() {
|
||||
// 任务:
|
||||
// 1. 模拟异步请求
|
||||
// 2. 1 秒后给 courses 赋值
|
||||
// 3. loading 改成 false
|
||||
},
|
||||
updated() {
|
||||
// 任务:在控制台输出 updated 日志
|
||||
},
|
||||
beforeDestroy() {
|
||||
// 任务:在控制台输出 beforeDestroy 日志
|
||||
},
|
||||
destroyed() {
|
||||
// 任务:在控制台输出 destroyed 日志
|
||||
},
|
||||
methods: {
|
||||
destroyInstance() {
|
||||
// 任务:调用 this.$destroy()
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user