首页 > web前端 > js教程 > 如何解决父组件中vuex方法更新state子组件不能及时更新并渲染

如何解决父组件中vuex方法更新state子组件不能及时更新并渲染

不言
发布: 2018-06-29 16:11:37
原创
2029 人浏览过

这篇文章主要介绍了父组件中vuex方法更新state子组件不能及时更新并渲染的完美解决方法,需要的朋友可以参考下

场景:

我实际用到的是这样的,我父组件引用子组件related,父组件调用获取页面详情的方法,更新了state值related,子组件根据该related来渲染相关新闻内容,但是页面打开的时候总是先加载子组件,子组件在渲染的时候还没有获取到更新之后的related值,即使在子组件中watch该值的变化依然不能渲染出来子组件的相关新闻内容。

我的解决办法:

父组件像子组件传值,当父组件执行了获取页面详情的方法之后,state值related更新,然后传给子组件,子组件再进行渲染,可以正常获取到。

父组件代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

<template>

 <p id="newsDetails">

  <mt-header title="详情">

   <router-link to="/" slot="left">

    <mt-button icon="back"></mt-button>

   </router-link>

  </mt-header>

  <p class="details clearfloat">

   <h1 class="titleFont">

    {{ title }}

   </h1>

   <p 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=&#39;btnFollow&#39; @click="follow">关注</span>

   </p>

   <p class="bodyFont clearfloat" id="bodyFont" ref="bodyFont" :class="{bodyHeight:contentStatus}">

    <p v-html="content"></p>

    <p class="editor" v-if="editorName">责任编辑:{{editorName}}</p>

   </p>

   <p class="contentToggle" @click="contentStatus=!contentStatus" v-if="contentStatus">阅读全文</p>

   <Related :related="related"></Related>

    <!--重点是这里 父组件向子组件传值-->

 </p> </p> </template>

 

import { Toast } from &#39;mint-ui&#39;;

 import {mapState} from &#39;vuex&#39;

 import Related from &#39;./Related.vue&#39;

 import moment from &#39;moment&#39;;

 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(&#39;zh-cn&#39;);

  },

  mounted(){

   setTimeout(()=>{

    this.contentToggle();

   },500)

  },

  watch: {

   &#39;$route&#39;(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(&#39;getDetails&#39;,this.id);

   },

   follow(){

    Toast(&#39;登录后进行关注&#39;);

    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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

<template>

  <p v-if="lists.length>0">

    <p class="tagTitle"><span>相关新闻</span></p>

    <p class="listItem" v-if="(item.type==&#39;little&#39;)" v-for="(item,index) in lists" :to="{name:&#39;details&#39;,params:{id:item.id}}" :key="index" @click="browserDetection()">

     <p class="listImg1">

      <!--<img :src="{lazy==loaded?item.thumb[0]:lazy==loading?&#39;../../assets/images/little_loading.png&#39;:lazy==error?&#39;../../assets/images/little_loading.png&#39;}" alt="" v-lazy="item.thumb[0]">-->

      <img :src="item.thumb[0]" alt="" v-lazy="item.thumb[0]">

     </p>

     <p class=&#39;titleBox1&#39;>

      <p class="listTitle">{{item.title}}</p>

      <p 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" rel="external nofollow" ></use>

       </svg>-->

       <span class="like">阅读 {{item.read}}</span>

       <span class="time">{{item.createAt|formatTime}}</span>

      </p>

    </p>

   </p>

  </p>

</template>

<script>

 import {mapActions, mapState, mapGetters} from &#39;vuex&#39;

 import moment from &#39;moment&#39;

 export default{

  data(){

   return {

    lists: [],

    id:this.$route.params.id,

   }

  },

  props:{

    related:Array  //重点是这里

  },

  created(){

   moment.locale(&#39;zh-cn&#39;);

  },

  /*computed: {

   ...mapState({

    related: state => state.newsDetails.related,

   })

  },*/

  filters:{

   formatTime(time){

    return moment(time).fromNow();

   },

  },

  methods:{

  },

  watch: {

   related (val) {

    this.lists = val;

   },

   &#39;$route&#39;(to,from){

    this.id=this.$route.params.id

   }

  }

 }

</script>

登录后复制

效果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

vue如何实现前进刷新后退不刷新的效果

关于Vue评论框架的实现(父组件的实现)

以上是如何解决父组件中vuex方法更新state子组件不能及时更新并渲染的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板