Skip to content

Commit 833dd96

Browse files
committed
feat[ptr]: vue-router 动态路由匹配
通过 vue-router 注入的原型对象 $route 提供的路由信息
1 parent 92046b4 commit 833dd96

12 files changed

Lines changed: 330 additions & 18 deletions

File tree

vue2/sandboxs/src/App.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<template>
22
<div id="app">
3-
<Pager
3+
<!-- <Pager
44
:current="current"
55
:total="total"
66
@pageChange="handlePageChange"
7-
></Pager>
7+
></Pager> -->
88
<!-- Vue 会自动将 $emit 的第二个参数传给处理函数的第一个参数 -->
99
<!-- @pageChange="(newPage) => handlePageChange(newPage)" -->
1010

11-
<p v-if="visible">v-if -> 没有vnnode -> 渲染节点数少</p>
11+
<!-- <p v-if="visible">v-if -> 没有vnnode -> 渲染节点数少</p>
1212
<p v-show="visible">v-show -> 始终有vnode=>DOM - 稳定</p>
13-
<button @click="changeVisible">切换显示</button>
13+
<button @click="changeVisible">切换显示</button> -->
1414

1515
<router-view></router-view>
1616
<router-link :to="{ name: 'Home' }" exact>主页</router-link>
@@ -21,10 +21,10 @@
2121
</template>
2222

2323
<script>
24-
import { Pager } from "./components";
24+
// import { Pager } from "./components";
2525
export default {
2626
components: {
27-
Pager,
27+
// Pager,
2828
},
2929
data() {
3030
return {

vue2/sandboxs/src/api/blog.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import request from "./request";
2+
3+
/**
4+
* 获取博客列表数据
5+
*/
6+
export async function getBlogs(page = 1, limit = 10, categoryid = -1) {
7+
return await request.get("/api/blog", {
8+
params: {
9+
page,
10+
limit,
11+
categoryid,
12+
},
13+
});
14+
}
15+
16+
/**
17+
* 获取博客分类
18+
*/
19+
export async function getBlogCategories() {
20+
return await request.get("/api/blogtype");
21+
}
22+

vue2/sandboxs/src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import App from './App.vue'
33

44
import './styles/global.less'
55

6+
import './mock' // 注意得在最开始配置mock拦截,后面目标XHR才会被扼杀
7+
68
import router from './router'
79
import { toast } from './utils'
810
Vue.prototype.$toast = toast
@@ -17,6 +19,4 @@ new Vue({
1719
}).$mount('#app')
1820

1921
// ========= 测试 =========
20-
21-
import './mock' // 注意得在最开始配置mock拦截,后面目标XHR才会被扼杀
2222
// import './api/test'

vue2/sandboxs/src/mixins/fetchData.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export default function (defaultDataValue = null) { // 不用函数的话设置
99
}
1010
},
1111
async created() {
12+
console.log("=== fetchData mixin created 钩子执行了");
1213
this.data = await this.fetchData();
14+
console.log("=== fetchData mixin 获取数据完成", this.data);
1315
this.isLoading = false;
1416
}
1517
}

vue2/sandboxs/src/mock/blog.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ Mock.mock(/^\/api\/blog(\?.+)?$/, "get", function (options) {
2424
[`rows|${query.limit || 10}`]: [
2525
{
2626
id: "@guid",
27-
title: "@ctitle",
27+
title: "@ctitle(1, 50)",
2828
description: "@cparagraph(1, 10)",
2929
category: {
3030
"id|1-10": 0,
3131
name: "分类@id",
3232
},
3333
"scanNumber|0-3000": 0,
3434
"commentNumber|0-300": 30,
35-
thumb: Mock.Random.image("300x250", "#000", "#fff", "Random Image"),
35+
"thumb|1": [
36+
Mock.Random.image("300x250", "#000", "#fff", "Random Image"),
37+
null,
38+
],
3639
createDate: `@date('T')`,
3740
},
3841
],

vue2/sandboxs/src/mock/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// AJAX 拦截规则
22
// 对目标 XHR 重新赋值
33
import Mock from 'mockjs'
4-
Mock.setup({
5-
timeout: "1000-2000"
6-
}) // 配置延迟、模拟异步
74

85
import './test'
6+
import './blog'
7+
8+
// import './test2' // 等后端开发完毕后直接注释导入即可
99

10-
// import './test2' // 等后端开发完毕后直接注释导入即可
10+
Mock.setup({
11+
timeout: "1000-2000"
12+
}) // 配置延迟、模拟异步

vue2/sandboxs/src/router/routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Message from '../views/Message'
66
export default [
77
{ name: "Home", path: "/", component: Home }, // 一个对象就是一条匹配规则
88
{ name: "About", path: "/about", component: About },
9-
{ name: "Blog", path: "/ariticle", component: Blog },
9+
{ name: "Blog", path: "/article", component: Blog },
10+
{ name: "CategoryBlog", path: "/article/cate/:categoryId", component: Blog },
1011
{ name: "Message", path: "/message", component: Message },
1112
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function (timestamp) {
2+
const date = new Date(+timestamp);
3+
const month = (date.getMonth() + 1).toString().padStart(2, "0");
4+
const day = date
5+
.getDate()
6+
.toString()
7+
.padStart(2, "0");
8+
return `${date.getFullYear()}-${month}-${day}`;
9+
}
10+

vue2/sandboxs/src/utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as toast } from './toast';
2-
export { default as getComponentRootDom } from './getComponentRootDom';
2+
export { default as getComponentRootDom } from './getComponentRootDom';
3+
export { default as formatDate } from './formatDate'
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<template>
2+
<div class="blog-category-container" v-loading="isLoading">
3+
<h2>文章分类</h2>
4+
<TreeListMenu :list="list" @select="handleSelect" />
5+
</div>
6+
</template>
7+
8+
<script>
9+
import TreeListMenu from "@/components/TreeListMenu";
10+
import fetchData from "@/mixins/fetchData.js";
11+
import { getBlogCategories } from "@/api/blog.js";
12+
export default {
13+
mixins: [fetchData([])],
14+
components: {
15+
TreeListMenu,
16+
},
17+
computed: {
18+
categoryId() {
19+
return +this.$route.params.categoryId || -1;
20+
},
21+
limit() {
22+
return +this.$route.query.limit || 10;
23+
},
24+
list() {
25+
const totalArticleCount = this.data.reduce(
26+
(a, b) => a + b.articleCount,
27+
0,
28+
);
29+
const result = [
30+
{ name: "全部", id: -1, articleCount: totalArticleCount },
31+
...this.data,
32+
];
33+
return result.map((it) => ({
34+
...it,
35+
isSelect: it.id === this.categoryId,
36+
aside: `${it.articleCount}`,
37+
}));
38+
},
39+
},
40+
methods: {
41+
async fetchData() {
42+
return await getBlogCategories();
43+
},
44+
handleSelect(item) {
45+
const query = {
46+
page: 1,
47+
limit: this.limit,
48+
};
49+
// 跳转到 当前的分类id 当前的页容量 newPage的页码
50+
if (item.id === -1) {
51+
this.$router.push({
52+
name: "Blog",
53+
query,
54+
});
55+
} else {
56+
this.$router.push({
57+
name: "CategoryBlog",
58+
query,
59+
params: {
60+
categoryId: item.id,
61+
},
62+
});
63+
}
64+
},
65+
},
66+
};
67+
</script>
68+
69+
<style scoped lang="less">
70+
.blog-category-container {
71+
width: 300px;
72+
box-sizing: border-box;
73+
padding: 20px;
74+
position: relative;
75+
height: 100%;
76+
overflow-y: auto;
77+
h2 {
78+
font-weight: bold;
79+
letter-spacing: 2px;
80+
font-size: 1em;
81+
margin: 0;
82+
}
83+
}
84+
</style>

0 commit comments

Comments
 (0)