父组件中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,
})
},
filters:{
formatTime(time){
return moment(time).fromNow();
},
},
methods:{
fetchData(){
this.$store.dispatch('getDetails',this.id);
},
follow(){
Toast('登录后进行关注');
this.$router.push("/login");
},
contentToggle(){
this.curHeight=this.$refs.bodyFont.offsetHeight;
if(parseFloat(this.curHeight)>parseFloat(this.bodyHeight)){
this.contentStatus=true;
}else{
this.contentStatus=false;
}
// this.hotCommentScrollTop=this.$refs.hotComment.height;
console.log(this.hotCommentScrollTop);
},
}
}
子组件related.vue
<template>
<div v-if="lists.length>0">
<div class="tagTitle"><span>相关新闻</span></div>
<div class="listItem" v-if="(item.type=='little')" v-for="(item,index) in lists" :to="{name:'details',params:{id:item.id}}" :key="index" @click="browserDetection()">
<div class="listImg1">
<!--<img :src="{lazy==loaded?item.thumb[0]:lazy==loading?'../../assets/images/little_loading.png':lazy==error?'../../assets/images/little_loading.png'}" alt="" v-lazy="item.thumb[0]">-->
<img :src="item.thumb[0]" alt="" v-lazy="item.thumb[0]">
</div>
<div class='titleBox1'>
<p class="listTitle">{{item.title}}</p>
<div class="titleInfo">
<span class="openApp">打开唐人家</span>
<span v-if="item.top==true" class="toTop">置顶</span>
<!--<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-dianzan"></use>
</svg>-->
<span class="like">阅读 {{item.read}}</span>
<span class="time">{{item.createAt|formatTime}}</span>
</div>
</div>
</div>
</div>
</template>
<script>
import {mapActions, mapState, mapGetters} from 'vuex'
import moment from 'moment'
export default{
data(){
return {
lists: [],
id:this.$route.params.id,
}
},
props:{
related:Array //重点是这里
},
created(){
moment.locale('zh-cn');
},
/*computed: {
...mapState({
related: state => state.newsDetails.related,
})
},*/
filters:{
formatTime(time){
return moment(time).fromNow();
},
},
methods:{
},
watch: {
related (val) {
this.lists = val;
},
'$route'(to,from){
this.id=this.$route.params.id
}
}
}
</script>
效果如图:

父组件中vuex方法更新state,子组件不能及时更新并渲染的解决方法的更多相关文章
- vue中父组件使用props或者$attras向子组件中传值
知识点:vue中使用props或者$attras向子组件中传值 (1) props传值 子组件必须注册好要传的数据() props:['id'] (2)$attrs传值 该数据在props中,没有注册 ...
- vue父组件更新,子组件也更新的方法
1.父组件 使用 Math.ramdom() 2.子组件获取 然后监听这个ramdom变化,处理子组件的更新
- Vue把父组件的方法传递给子组件调用(评论列表例子)
Vue把父组件的方法传递给子组件调用(评论列表例子) 效果展示: 相关Html: <!DOCTYPE html> <html lang="en"> < ...
- Vue父组件向子组件传递方法(自定义方法)并且子组件向父组件传递数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue父组件引用多个相同的子组件传值
没有什么问题是for 解决不了的,我一直深信这句话,当然这句话也是我说的 父组件引用多个相同的子组件传值问题 (这种情况很少遇到) 1 <template> 2 <div> 3 ...
- <style scoped >中使用深度选择器影响子组件
摘自:https://blog.csdn.net/zhouzuoluo/article/details/95593143 <style scoped >中使用深度选择器影响子组件 在< ...
- ios系统微信浏览器、safari浏览器中h5页面上拉下滑导致悬浮层脱离窗口的解决方法
一. 运行环境: iphone所有机型的qq浏览器,safari浏览器,微信内置浏览器(qq浏览器内核)等. 二. 异常现象: 1. 大幅度上下滑动h5页面,然后停止滑动,有时候会影响到页面滚动,如局 ...
- 在Eclipse中运行Jboss时出现java.lang.OutOfMemoryError:PermGen space及其解决方法
在Eclipse中运行Jboss时出现java.lang.OutOfMemoryError:PermGen space及其解决方法 在Eclipse中运行Jboss时,时间太长可能有时候会出现java ...
- ckfinder在IE10,IE9中的弹出框不能选择,或者不能上传解决方法
在IE9,或IE10中ckfinder在IE10,IE9中的弹出框不能选择,或者不能上传解决方法 把弹出框嵌入到jquery dialog中.可以解决 I did: // javascript f ...
随机推荐
- 基于HTML5手机上下滑动翻页特效
基于HTML5手机上下滑动翻页特效.这是一款手机移动端触屏滑动翻页代码下载.效果图如下: 在线预览 源码下载 实现的代码. html代码: <section class="u-al ...
- Java多线程——sychronized
概述 关键字synchronized的作用是实现线程间的同步.它的工作是对同步的代码加锁,使得每一次,只能有一个线程进入同步块,从而保证线程间的安全性. 直接作用于实例方法(普通同步方法):对当前实例 ...
- <花荣《至尊狐狸》中国股市精英最优套利战术>读书笔记
书在这里 第一定律:博弈分析是一切实战操作的基础,资金的机会利润由主力投机状态决定,资金的风险承受度由投资标的价值底线锁定 第二定律:套利战术应是背景性的.主动性的.互动性的,是最安全最保守的.最明确 ...
- JavaScript 里面的整数 位 操作
JavaScript 整数位操作. 与 操作符 & val num1 = 10; val num2 = 11; val num3 = num1 & num2; // num3 == 1 ...
- CAS (14) —— CAS 更多用户信息
CAS (14) -- CAS 更多用户信息 摘要 将更多用户信息写入到service验证返回消息中 版本 tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0_65 cas ...
- 编译JDK源代码【转】
用Eclipse Debug,当跟踪进jdk api里时(比如javax.swing包里的类),无法查看某些local filed的值.这是因为jdk里的代码在打包时删除了一些用于调试的信息,以减小安 ...
- 协变(covariant)和逆变(contravariant)
我们知道子类转换到父类,在C#中是能够隐式转换的.这种子类到父类的转换就是协变. 而另外一种类似于父类转向子类的变换,可以简单的理解为“逆变”. 上面对逆变的简单理解有些牵强,因为协变和逆变只能针对接 ...
- mssqlserver获取表说明和行数
SELECT a.*,t.rows FROM ( ) ) AS a left join (, )) ) AS t ON a.表名=t.name
- SpringMVC深度探险(三) —— DispatcherServlet与初始化主线
在上一篇文章中,我们给出了构成SpringMVC应用程序的三要素以及三要素的设计过程.让我们来归纳一下整个设计过程中的一些要点: SpringMVC将Http处理流程抽象为一个又一个处理单元 Spri ...
- Zookeeper 快速理解
转自:http://blog.csdn.net/colorant/article/details/8444283 == 是什么 == 目标Scope(解决什么问题) 为分布式系统提供高可靠性的协同工作 ...