欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

父組件中vuex方法更新state子組件不能及時(shí)更新并渲染的完美解決方法

 更新時(shí)間:2018年04月25日 09:02:24   作者:蓓蕾心晴  
這篇文章主要介紹了父組件中vuex方法更新state子組件不能及時(shí)更新并渲染的完美解決方法,需要的朋友可以參考下

場景:

我實(shí)際用到的是這樣的,我父組件引用子組件related,父組件調(diào)用獲取頁面詳情的方法,更新了state值related,子組件根據(jù)該related來渲染相關(guān)新聞內(nèi)容,但是頁面打開的時(shí)候總是先加載子組件,子組件在渲染的時(shí)候還沒有獲取到更新之后的related值,即使在子組件中watch該值的變化依然不能渲染出來子組件的相關(guān)新聞內(nèi)容。

我的解決辦法:

父組件像子組件傳值,當(dāng)父組件執(zhí)行了獲取頁面詳情的方法之后,state值related更新,然后傳給子組件,子組件再進(jìn)行渲染,可以正常獲取到。

父組件代碼:

<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">關(guān)注</span>
   </div>
   <div class="bodyFont clearfloat" id="bodyFont" ref="bodyFont" :class="{bodyHeight:contentStatus}">
    <div v-html="content"></div>
    <div class="editor" v-if="editorName">責(zé)任編輯:{{editorName}}</div>
   </div>
   <div class="contentToggle" @click="contentStatus=!contentStatus" v-if="contentStatus">閱讀全文</div>
   <Related :related="related"></Related>
    <!--重點(diǎn)是這里 父組件向子組件傳值-->
 </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('登錄后進(jìn)行關(guān)注');
    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>相關(guān)新聞</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" rel="external nofollow" ></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  //重點(diǎn)是這里
  },
  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>

效果如圖:

總結(jié)

以上所述是小編給大家介紹的父組件中vuex方法更新state子組件不能及時(shí)更新并渲染的完美解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 解決Vue3報(bào)錯(cuò):Property?“xxx“?was?accessed?during?render?but?is?not?defined?on?instance.

    解決Vue3報(bào)錯(cuò):Property?“xxx“?was?accessed?during?render?but

    這篇文章主要給大家介紹了關(guān)于解決Vue3報(bào)錯(cuò):Property?“xxx“?was?accessed?during?render?but?is?not?defined?on?instance.的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Vue3監(jiān)聽reactive對象中屬性變化的方法

    Vue3監(jiān)聽reactive對象中屬性變化的方法

    在 Vue 3 中,如果你想監(jiān)聽 reactive 對象中的某個(gè)屬性發(fā)生的變化,你可以使用 watch 函數(shù)進(jìn)行監(jiān)聽,watch 函數(shù)允許你觀察 reactive 對象的某個(gè)屬性或者整個(gè)對象,所以本文給大家介紹了Vue3監(jiān)聽reactive對象中屬性變化的方法,需要的朋友可以參考下
    2024-08-08
  • 記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn)

    記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn)

    這篇文章主要介紹了記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Vue使用視頻作為網(wǎng)頁背景的實(shí)現(xiàn)指南

    Vue使用視頻作為網(wǎng)頁背景的實(shí)現(xiàn)指南

    在現(xiàn)代網(wǎng)頁設(shè)計(jì)中,視頻背景逐漸成為一種流行的設(shè)計(jì)趨勢,它不僅能夠提升網(wǎng)頁的動(dòng)態(tài)效果,還可以在視覺上抓住用戶的注意力,本文將詳細(xì)講解如何在頁面中使用視頻作為背景,并確保內(nèi)容可見、頁面元素布局合理,需要的朋友可以參考下
    2024-10-10
  • 淺談vue限制文本框輸入數(shù)字的正確姿勢

    淺談vue限制文本框輸入數(shù)字的正確姿勢

    這篇文章主要介紹了vue限制文本框輸入數(shù)字的正確姿勢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 前端Vue3項(xiàng)目打包成Docker鏡像運(yùn)行的詳細(xì)步驟

    前端Vue3項(xiàng)目打包成Docker鏡像運(yùn)行的詳細(xì)步驟

    將Vue3項(xiàng)目打包、編寫Dockerfile、構(gòu)建Docker鏡像和運(yùn)行容器是部署Vue3項(xiàng)目到Docker的主要步驟,這篇文章主要介紹了前端Vue3項(xiàng)目打包成Docker鏡像運(yùn)行的詳細(xì)步驟,需要的朋友可以參考下
    2024-09-09
  • vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例

    vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例

    本篇文章主要介紹了vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • vue使用websocket實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)推送功能

    vue使用websocket實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)推送功能

    這篇文章主要為大家詳細(xì)介紹了vue如何使用websocket實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)推送,發(fā)布訂閱重連單點(diǎn)登錄功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • Vue項(xiàng)目使用svg圖標(biāo)實(shí)踐

    Vue項(xiàng)目使用svg圖標(biāo)實(shí)踐

    這篇文章主要介紹了Vue項(xiàng)目使用svg圖標(biāo)實(shí)踐,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 淺析Vue自定義組件的v-model

    淺析Vue自定義組件的v-model

    這篇文章主要介紹了Vue之徹底理解自定義組件的v-model的相關(guān)知識 ,需要的朋友可以參考下的相關(guān)資料
    2017-11-11

最新評論