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" >
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 ;
0 commit comments