POSTLIST部分在页面展示。。。。。
来源:7-7 使用vuex action 发送异步请求第二部分
weixin_慕斯卡6285484
2022-08-03
post list部分在第一次老师展示不出来,但是数据已经返回了,必须要退回再点击页面才会显示?是为什么????
POSTLIST组件
<template>
<div class="post-list">
<article v-for="item in list" :key="item._id" class="card mb-3 shadow-sm">
<div class="card-body">
<h4>{{item.title}}</h4>
<div class="row my-3 align-items-center">
<div v-if="item.image" class="col-3">
<img :src="item.image&&item.image.url" :alt="item.title" class="rounded-lg w-100">
</div>
<p :class="{'col-9': item.image}">{{item.excerpt}}</p>
</div>
<span class="text-muted">{{item.createdAt}}</span>
</div>
</article>
</div>
</template>
<script lang="ts">
import { defineComponent,PropType,computed } from "vue";
import { useStore } from "vuex";
import { PostProps} from "../store";
export default defineComponent({
// props是干啥的?components是干啥的?
props:{
list:{
required:true,
// protype?
type:Array as PropType<PostProps[]>
}
},
setup() {
const store=useStore()
const isloading=computed(()=>store.state.loading)
return {isloading}
}
})
</script>
<style>
</style>
columndetail组件
在<template>
<div class="column-detail-page w-75 mx-auto ">
<div class="column-info row mb-4 border-bottom pb-4 align-items-center">
<div class="col-3 text-center">
<img :src="column.avatar&&column.avatar.url" :alt="column.title" class="rounded-circle border">
</div>
<div class="col-9">
<h4>{{column.title}}</h4>
<p>{{column.description}}</p>
</div>
</div>
<post-list :list="list"></post-list>
</div>
</template>
<script lang="ts">
import { defineComponent, onMounted } from "vue";
import { useRoute} from "vue-router";
import { useStore } from "vuex";
import {GlobalDataProps} from "../store";
// import { Columnprops } from "./ColumnList.vue";
// import { columnData,paperData } from "@/testData";
import PostList from "./PostList.vue";
export default defineComponent({
components: { PostList },
setup() {
const route=useRoute()
const store=useStore<GlobalDataProps>()
// 获取当下页面的id
const currentId=route.params.id
onMounted(() => {
store.dispatch('fetchColumn',currentId)
store.dispatch('fetchPosts',currentId)
})
// 专栏的ids
// 这块要用泛型声明一下
const column=store.getters. getColumnById(currentId)
const list=store.getters.getPostByCid(currentId)
console.log('aaaa',store.state.paper);
console.log('bbb',currentId);
console.log('cccc',store.getters. getColumnById(currentId));
return {
column,
list,
}
}
})
</script>
<style>
</style>
写回答
2回答
-
张轩
2022-08-04
同学你好 经查 是因为你的 getters 没有添加 computed,所以响应性失效了,添加上就好了
// ColumnDetail 51行 52行 const column=computed(() => store.getters.getColumnById(currentId)) const list=computed(() => store.getters.getPostByCid(currentId)) // 还有一个小问题 第三行最后面添加一个 v-if 可以杜绝一些js错误信息 <div class="column-info row mb-4 border-bottom pb-4 align-items-center" v-if="column">
亲测可用~
112022-08-05 -
张轩
2022-08-03
同学你好
请提供一下你的源代码(git 地址)吧,我在本地帮你看一下,谢谢~
022022-08-04
相似问题