Skip to content

Commit 515e8a9

Browse files
committed
feat: 交互循环
1 parent 8b5ff87 commit 515e8a9

4 files changed

Lines changed: 37 additions & 17 deletions

File tree

vue2/sandboxs/src/components/TreeListMenu/index.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ Vue 的 template 是静态的,不像 React ,如果想要实现嵌套结构
1111
<span :class="{ selected: item.isSelected }" @click="handleClick(item)">{{
1212
item.name
1313
}}</span>
14+
<span
15+
v-if="item.aside"
16+
@click="handleClick(item)"
17+
class="aside"
18+
:class="{ selected: item.isSelected }"
19+
>
20+
{{ item.aside }}
21+
</span>
1422
<TreeListMenu :list="item.children" @click="handleClick">
1523
<!-- 归的时候从子组件上抛的事件继续往上抛 -->
1624
</TreeListMenu>

vue2/sandboxs/src/views/Blog/components/BlogCategory.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="blog-category-container" v-loading="isLoading">
33
<h2>文章分类</h2>
4-
<TreeListMenu :list="list" @select="handleSelect" />
4+
<TreeListMenu :list="list" @click="handleClick" />
55
</div>
66
</template>
77

@@ -11,6 +11,7 @@ import fetchData from "@/mixins/fetchData.js";
1111
import { getBlogCategories } from "@/api/blog.js";
1212
export default {
1313
mixins: [fetchData([])],
14+
// 只需要 mixins 中 created 的初始化一次即可,因为分类的数据不像 分页数据 一样会被路由数据影响
1415
components: {
1516
TreeListMenu,
1617
},
@@ -24,15 +25,15 @@ export default {
2425
list() {
2526
const totalArticleCount = this.data.reduce(
2627
(a, b) => a + b.articleCount,
27-
0,
28+
0, // reduce((pre, now) => 操作, preDefault)
2829
);
2930
const result = [
3031
{ name: "全部", id: -1, articleCount: totalArticleCount },
3132
...this.data,
3233
];
3334
return result.map((it) => ({
3435
...it,
35-
isSelect: it.id === this.categoryId,
36+
isSelected: it.id === this.categoryId, // 通过路由信息 => 选中
3637
aside: `${it.articleCount}`,
3738
}));
3839
},
@@ -41,7 +42,8 @@ export default {
4142
async fetchData() {
4243
return await getBlogCategories();
4344
},
44-
handleSelect(item) {
45+
handleClick(item) {
46+
// 注意方法名需要和递归组件中一致,否则上抛无法处理
4547
const query = {
4648
page: 1,
4749
limit: this.limit,
@@ -68,12 +70,12 @@ export default {
6870

6971
<style scoped lang="less">
7072
.blog-category-container {
71-
width: 300px;
73+
width: 250px;
7274
box-sizing: border-box;
7375
padding: 20px;
7476
position: relative;
7577
height: 100%;
76-
overflow-y: auto;
78+
overflow-y: auto; // 别忘记滚动条
7779
h2 {
7880
font-weight: bold;
7981
letter-spacing: 2px;

vue2/sandboxs/src/views/Blog/components/BlogList.vue

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<template>
22
<div class="blog-list-container" ref="container" v-loading="isLoading">
3+
<!-- isLoading 是从混入 fetchData 中拿到的 -->
4+
<!-- Vue3 的 composition 组合式更便利 -->
35
<ul>
46
<li v-for="item in data.rows" :key="item.id">
57
<div class="thumb" v-if="item.thumb">
@@ -24,7 +26,8 @@
2426
</div>
2527
</li>
2628
</ul>
27-
<!-- 分页放到这里 -->
29+
<!-- 路由信息 => 分页参数 -->
30+
<!-- fetchData => 总数据量 -->
2831
<Pager
2932
v-if="data.total"
3033
:current="routeInfo.page"
@@ -63,7 +66,7 @@ export default {
6366
formatDate, // 导入后记得配置到组件中,否则模版中不能用
6467
async fetchData() {
6568
console.log("=== BlogList fetchData 方法被调用", this.routeInfo);
66-
// return
69+
// 将路由信息传入 fetch 参数
6770
const res = await getBlogs(
6871
this.routeInfo.page,
6972
this.routeInfo.limit,
@@ -72,6 +75,7 @@ export default {
7275
console.log("=== BlogList res", res);
7376
return res;
7477
},
78+
// 页码变化 => 改变路由
7579
handlePageChange(newPage) {
7680
const query = {
7781
page: newPage,
@@ -86,6 +90,7 @@ export default {
8690
query,
8791
});
8892
} else {
93+
// 有分类的话得传入参数
8994
// /article/cate/${this.routeInfo.categoryId}?page=${newPage}&limit=${this.routeInfo.limit}
9095
this.$router.push({
9196
name: "CategoryBlog",
@@ -98,12 +103,15 @@ export default {
98103
},
99104
},
100105
watch: {
101-
async $route() {
102-
this.isLoading = true;
103-
// 滚动高度为0
104-
this.$refs.container.scrollTop = 0;
105-
this.data = await this.fetchData();
106-
this.isLoading = false;
106+
// 观察路由this.$route ⇒ fetch new Data
107+
$route: {
108+
async handler() {
109+
this.isLoading = true;
110+
// 设置滚动高度为0 ,往上切
111+
this.$refs.container.scrollTop = 0;
112+
this.data = await this.fetchData(); // 不需要参数,fetchData 内部就用的 计算属性
113+
this.isLoading = false;
114+
},
107115
},
108116
},
109117
};
@@ -119,7 +127,7 @@ export default {
119127
width: 100%;
120128
height: 100%;
121129
box-sizing: border-box;
122-
scroll-behavior: smooth;
130+
scroll-behavior: smooth; // 平滑滚动
123131
ul {
124132
list-style: none;
125133
margin: 0;

vue2/sandboxs/src/views/Blog/index.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
<!-- 交互循环:路由信息 ⇒ 激活状态、文章列表 ⇒ 用户点击操作 ⇒ 更改路由信息 -->
2+
13
<template>
24
<div class="blog-container">
3-
<h1>
5+
<!-- <h1>
46
<a href="https://blog.csdn.net/2301_78856868"
57
>Welcome to ceilf6's Blogs</a
68
>
79
<span v-if="$route.params.categoryId">
810
分类ID: {{ $route.params.categoryId }}
911
</span>
10-
</h1>
12+
</h1> -->
1113
<ThreeColumnLayout>
1214
<!-- 注意 ThreeColumnLayout 中的 main 是类名、不是插槽 name -->
1315
<BlogList></BlogList>

0 commit comments

Comments
 (0)