没有合适的资源?快使用搜索试试~ 我知道了~
首页父组件中vuex方法更新state子组件不能及时更新并渲染的完美解决方法
父组件中vuex方法更新state子组件不能及时更新并渲染的完美解决方法
6 下载量 140 浏览量
更新于2023-03-16
评论
收藏 97KB PDF 举报
主要介绍了父组件中vuex方法更新state子组件不能及时更新并渲染的完美解决方法,需要的朋友可以参考下
资源详情
资源评论
资源推荐
父组件中父组件中vuex方法更新方法更新state子组件不能及时更新并渲染的完美解决方法子组件不能及时更新并渲染的完美解决方法
主要介绍了父组件中vuex方法更新state子组件不能及时更新并渲染的完美解决方法,需要的朋友可以参考下
场景:场景:
我实际用到的是这样的,我父组件引用子组件related,父组件调用获取页面详情的方法,更新了state值related,子组件根据该related来渲染相关新闻内
容,但是页面打开的时候总是先加载子组件,子组件在渲染的时候还没有获取到更新之后的related值,即使在子组件中watch该值的变化依然不能渲染
出来子组件的相关新闻内容。
我的解决办法:我的解决办法:
父组件像子组件传值,当父组件执行了获取页面详情的方法之后,state值related更新,然后传给子组件,子组件再进行渲染,可以正常获取到。
父组件代码:父组件代码:
<template>
<div id="newsDetails">
<mt-header title="详情">
<router-link to="/" slot="left">
<mt-button icon="back"></mt-button>
</router-link>
</mt-header>
<div class="details clearfloat">
<h1 class="titleFont">
{{ title }}
</h1>
<div class="clearfloat sourceWrap">
<ul class="sourceFont">
<li v-if="(pubNews==true)">
<span class="source">{{pubName}}</span>
</li>
<li>
<span class="authorName">{{authorName}}</span>
<span class="time">{{createAt|formatTime}}</span>
</li>
</ul>
<span v-if="(pubNews==true)" class='btnFollow' @click="follow">关注</span>
</div>
<div class="bodyFont clearfloat" id="bodyFont" ref="bodyFont" :class="{bodyHeight:contentStatus}">
<div v-html="content"></div>
<div class="editor" v-if="editorName">责任编辑:{{editorName}}</div>
</div>
<div class="contentToggle" @click="contentStatus=!contentStatus" v-if="contentStatus">阅读全文</div>
<Related :related="related"></Related>
<!--重点是这里 父组件向子组件传值-->
</div> </div> </template>
import { Toast } from 'mint-ui';
import {mapState} from 'vuex'
import Related from './Related.vue'
import moment from 'moment';
export default{
name:"NewsDetails",
components:{
Related,
},
data(){
return {
id:this.$route.params.id,
topicType:"news",
contentStatus:false,
curHeight:0,
bodyHeight:5000,
hotCommentScrollTop:0
}
},
created(){
this.id=this.$route.params.id;
this.fetchData();
moment.locale('zh-cn');
},
mounted(){
setTimeout(()=>{
this.contentToggle();
},500)
},
watch: {
'$route'(to,from){
this.id=this.$route.params.id;
this.fetchData();
}
},
computed: {
...mapState({
title: state => state.newsDetails.title,
authorName: state => state.newsDetails.authorName,
pubNews: state => state.newsDetails.pubNews,
pubName: state => state.newsDetails.pubName,
editorName: state => state.newsDetails.editorName,
createAt: state => state.newsDetails.createAt,
content: state => state.newsDetails.content,
myFavourite: state => state.newsDetails.myFavourite,
related: state => state.newsDetails.related,
weixin_38500090
- 粉丝: 4
- 资源: 964
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0