topic.vue
2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<template>
<Article ref="article" :on-fetch="onFetch" @on-follow="onFollow"></Article>
</template>
<script>
import {get} from 'lodash';
import Article from './components/article/article';
import ArticleItem from './components/article/article-item';
import {createNamespacedHelpers} from 'vuex';
const {mapState, mapActions, mapMutations} = createNamespacedHelpers('article');
export default {
name: 'ArticlePage',
data() {
return {
page: 1,
labelId: 0,
reload: true
};
},
created() {
this.labelId = this.$route.params.labelId;
},
activated() {
if (this.$route.params.labelId !== this.labelId) {
console.log('activated init');
this.labelId = this.$route.params.labelId;
this.init();
}
},
beforeRouteUpdate(to, from, next) {
if (this.$route.params.labelId !== to.params.labelId) {
this.labelId = to.params.labelId;
this.init();
}
next();
},
computed: {
...mapState(['articleListByTopic']),
currentList() {
if (this.articleListByTopic.length > 2) {
return this.articleListByTopic.slice(0, 2);
}
return this.articleListByTopic;
}
},
methods: {
...mapMutations(['CHANGE_AUTHOR_TOPIC_FOLLOW']),
...mapActions(['fetchArticleListByTopic']),
init() {
this.page = 1;
this.$refs.article.init();
},
async onFetch() {
const labelId = parseInt(this.labelId, 10);
if (!labelId) {
return;
}
const result = await this.fetchArticleListByTopic({
labelId,
page: this.page
});
if (result.code === 200) {
if (get(result, 'data.detailList', []).length) {
this.page++;
return Promise.resolve(result.data.detailList);
}
return Promise.resolve(false);
} else {
this.$createToast && this.$createToast({
txt: result.message || '服务器开小差了',
type: 'warn',
time: 1000
}).show();
}
},
onFollow({authorUid, follow}) {
this.CHANGE_AUTHOR_TOPIC_FOLLOW({authorUid: authorUid, follow});
}
},
components: {
Article,
ArticleItem
}
};
</script>
<style lang="scss" scoped>
.article-page {
background-color: #f0f0f0;
}
</style>