我的帖子页面底部有一个相关内容部分,其中显示其他相关帖子。
当点击相关内容时,我希望路由器能够更新页面。然而,这并没有发生。 url 正在更改,但视图不会更新。
组件
Post.Vue
<template>
<div class="post-container" >
<router-view name="PostContent">
<h2>test</h2>
</router-view>
<div v-if="currentPost !== ''">
<img :src="currentPost.jetpack_featured_media_url" />
<!-- <h1 v-html="currentPost.title.rendered"></h1> -->
<div
v-html="currentPost.excerpt.rendered"
class="post-excerpt-container"
></div>
<div
v-html="currentPost.content.rendered"
class="post-content-container"
></div>
</div>
<section class="related-content">
<h2>Related Content:</h2>
<p v-if="currentPost.title !== undefined">If you enjoyed {{currentPost.title.rendered}}, we think you'll like:</p>
<div class="related-content-container" v-for="relatedPost in relatedPosts" :key="relatedPost.id" :data-id="relatedPost.id">
<router-link :to="{name:'Post',params:{id:relatedPost.id}}">
<RelatedCard :post='relatedPost' />
</router-link>
</div>
</section>
</div>
</template>
<script>
import { mapState } from "vuex";
import RelatedCard from '@/components/RelatedCard.vue';
export default {
name:"Post",
components:{RelatedCard},
data() {
return {
currentPost: "",
id: this.$route.params.id,
relatedPosts: []
};
},
computed: {
...mapState({
baseAPIURL: (state) => state.baseAPIURL,
posts: (state) => state.posts,
}),
},
created() {
console.log('created')
fetch(`${this.baseAPIURL}/posts/${this.id}?_embed`)
.then((resp) => resp.json())
.then((post) => {
this.currentPost = post;
});
},
methods: {
pushToRelated() {
this.posts.forEach((post) => {
post.relatedScore = 0;
if (post.id !== this.currentPost.id) {
post._embedded['wp:term'][0].forEach(el=>{
for(let i = 0;i < this.currentPost._embedded['wp:term'][0].length;i++){
if (el.name === this.currentPost._embedded['wp:term'][0][i].name){
post.relatedScore = post.relatedScore + 3
}
}
})
post._embedded['wp:term'][1].forEach(el=>{
for(let i = 0;i < this.currentPost._embedded['wp:term'][1].length;i++){
if (el.name === this.currentPost._embedded['wp:term'][1][i].name){
post.relatedScore = post.relatedScore + 1
}
}
})
}
});
this.relatedPosts = this.posts.sort((a,b)=>a.relatedScore - b.relatedScore).reverse().slice(0,5)
}
},
watch: {
currentPost: function () {
if (this.posts.length > 0){
this.pushToRelated();
}
},
}
};
</script>
RelatedCard.vue
<template>
<div class="related-card">
<div>
<img v-if="post._embedded['wp:featuredmedia'][0].media_details.sizes.medium_large !== undefined" :src="postImageML" alt="" />
<img v-else :src="postImage" alt="">
</div>
<div>
<h2 v-html="post.title.rendered"></h2>
<p v-html="post.excerpt.rendered"></p>
</div>
</div>
</template>
<script>
export default {
props: {
post: Object,
},
computed:{
postImageML(){
return this.post._embedded['wp:featuredmedia'][0].media_details.sizes.medium_large.source_url
},
postImage(){
return this.post._embedded['wp:featuredmedia'][0].media_details.sizes.full.source_url
}
},
};
</script>
我的路由器配置
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
{
path: "/list",
name: "List",
component: List,
},
{ path: "/database", name: "Database", component: Database },
{path:"/post/:id",name:"Post",component:Post}
];
const router = new VueRouter({
routes,
});
export default router;
我尝试过的:
我尝试过使用 $router.go()。这有助于更新页面。然而,重新渲染后,我的观察者无法正常工作。另外,我失去了用户返回上一篇文章的能力。
我还尝试在包装元素上使用组件键,但我没有运气。
任何关于为什么会发生这种情况以及我可以采取哪些措施来解决它的指示都会很棒。
当您输入当前正在渲染组件的路由时,默认情况下会重用该组件,因此不会调用
created
。你可以:
:key
使用密钥意味着如果密钥不匹配,组件将不会被重复使用。将您的路由器视图更改为:
<router-view :key="$route.fullPath" />
每个单独的参数都会更改路由器的完整路径并给出唯一的密钥。
updated
挂钩与
created
不同,每当参数更改时都会调用此函数。但它不会像 created
那样在第一次加载时被调用,所以你需要使用两个钩子。
created() {
// Do whatever when the component loads
},
updated() {
// Do whatever when the component updates
}
我还没有阅读你的所有代码,但尝试将历史模式添加到路由器。
const router = new VueRouter({
routes,
mode: 'history', // Add this
});