Skip to content

Commit

Permalink
feat: article-list拆分组件
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Oct 31, 2023
1 parent 88a0735 commit cc3a943
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 53 deletions.
83 changes: 83 additions & 0 deletions packages/home/src/components/home/article/article-list.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<template>
<div class="freyja-article-list">
<article
v-for="article in articles"
:key="article._id"
>
<h3 class="freyja-article-title">
<router-link :to="{name: 'article', params: {id: article._id}}">
{{ article.title }}
</router-link>
</h3>
<div class="freyja-article-info freyja-article-time">
<hr>
<span class="time"><i class="el-icon-time" /> {{ formatDate(article.createdAt) }}</span>
<span class="comments">
<i
v-if="article.commentCount === 0"
class="fa fa-comments"
>并没有评论</i>
<i
v-else
class="fa fa-comments"
>有{{ article.commentCount }}条评论</i>
</span>
</div>
<div class="freyja-article-summary freyja-article-content">
<div v-html="article.summary" />
</div>
<hr>
</article>
</div>
</template>
<script lang="ts" setup>
import lozad from 'lozad'
import prismjs from 'prismjs'
import {onMounted, onUpdated, ref} from 'vue'
import {IArticle} from '../../../store/home.ts'
const props = defineProps<{
articles: IArticle[]
}>()
const articles = ref(props.articles)
onMounted(() => {
highlight()
const observer = lozad()
observer.observe()
})
onUpdated(() => {
highlight()
})
function highlight(): void {
prismjs.highlightAll()
}
function formatDate(date: Date | string): string {
if (typeof date === 'string') {
date = new Date(date)
}
return date.toLocaleString()
}
</script>
<style lang="scss">
.freyja-article-pager-prev {
float: left;
width: 300px;
max-width: 40%;
}
.freyja-article-pager-next {
float: right;
width: 300px;
max-width: 40%;
}
.freyja-article-info {
.time {
margin-right: 20px;
}
}
</style>
56 changes: 3 additions & 53 deletions packages/home/src/pages/home/HomeIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,7 @@
<h2 v-if="keyword">
关键字: {{ keyword }}
</h2>
<div class="freyja-article-list">
<article
v-for="article in articles"
:key="article._id"
>
<h3 class="freyja-article-title">
<router-link :to="{name: 'article', params: {id: article._id}}">
{{ article.title }}
</router-link>
</h3>
<div class="freyja-article-info freyja-article-time">
<hr>
<span class="time"><i class="el-icon-time" /> {{ formatDate(article.createdAt) }}</span>
<span class="comments">
<i
v-if="article.commentCount === 0"
class="fa fa-comments"
>并没有评论</i>
<i
v-else
class="fa fa-comments"
>有{{ article.commentCount }}条评论</i>
</span>
</div>
<div class="freyja-article-summary freyja-article-content">
<div v-html="article.summary" />
</div>
<hr>
</article>
</div>
<article-list :articles="articles" />
<div class="freyja-article-pager">
<router-link :to="{query: {page: page-1}}">
<el-button
Expand All @@ -62,10 +33,9 @@
</template>
<script lang="ts" setup>
import {ElButton} from 'element-plus'
import lozad from 'lozad'
import prismjs from 'prismjs'
import {computed, onMounted, onUpdated, ref, watch} from 'vue'
import {computed, ref, watch} from 'vue'
import {useRoute} from 'vue-router'
import ArticleList from '../../components/home/article/article-list.vue'
import {IArticle, useHomeStore} from '../../store/home.ts'
const route = useRoute()
Expand Down Expand Up @@ -108,26 +78,6 @@ watch(route, async () => {
const canGoBackward = computed(() => page > 1)
const canGoForward = computed(() => homeStore.articles.length === 20)
onMounted(() => {
highlight()
const observer = lozad()
observer.observe()
})
onUpdated(() => {
highlight()
})
function highlight(): void {
prismjs.highlightAll()
}
function formatDate(date: Date | string): string {
if (typeof date === 'string') {
date = new Date(date)
}
return date.toLocaleString()
}
</script>
<style lang="scss">
.freyja-article-pager-prev {
Expand Down

0 comments on commit cc3a943

Please sign in to comment.