最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

場景:

我實際用到的是這樣的,我父組件引用子組件related,父組件調(diào)用獲取頁面詳情的方法,更新了state值related,子組件根據(jù)該related來渲染相關(guān)新聞內(nèi)容,但是頁面打開的時候總是先加載子組件,子組件在渲染的時候還沒有獲取到更新之后的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子組件不能及時更新并渲染的完美解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vue分頁組件實例代碼

    Vue分頁組件實例代碼

    這篇文章主要為大家詳細(xì)介紹了Vue分頁組件的實例代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • vue3項目中引入ts的詳細(xì)圖文教程

    vue3項目中引入ts的詳細(xì)圖文教程

    最近項目需要將原vue項目結(jié)合ts的使用進(jìn)行改造,這個后面應(yīng)該是中大型項目的發(fā)展趨勢,下面這篇文章主要給大家介紹了關(guān)于vue3項目中引入ts的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • Vue 3.0中Treeshaking特性及作用

    Vue 3.0中Treeshaking特性及作用

    Tree shaking 是一種通過清除多余代碼方式來優(yōu)化項目打包體積的技術(shù),就是在保持代碼運(yùn)行結(jié)果不變的前提下,去除無用的代碼,本文給大家介紹Vue 3.0中Treeshaking特性是什么,感興趣的朋友一起看看吧
    2023-10-10
  • 使用canvas實現(xiàn)一個vue彈幕組件功能

    使用canvas實現(xiàn)一個vue彈幕組件功能

    這篇文章主要介紹了使用canvas實現(xiàn)一個vue彈幕組件功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Vue如何下載本地靜態(tài)資源static文件夾

    Vue如何下載本地靜態(tài)資源static文件夾

    這篇文章主要介紹了Vue如何下載本地靜態(tài)資源static文件夾,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Thymeleaf?+?Vue組件化開發(fā)方式

    Thymeleaf?+?Vue組件化開發(fā)方式

    這篇文章主要介紹了Thymeleaf?+?Vue組件化開發(fā)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue?通過this.$emit()方法子組件向父組件傳值(步驟分享)

    Vue?通過this.$emit()方法子組件向父組件傳值(步驟分享)

    這篇文章主要介紹了Vue?this.$emit()方法通過子組件向父組件傳值,第一步在父組件中引入子組件,第二步子組件向父組件傳值,本文通過需要的朋友可以參考下
    2022-11-11
  • 一文秒懂Vue3的v-model

    一文秒懂Vue3的v-model

    v-model 是 Vue 內(nèi)置的指令作為屬性接收一個變量(不能是常量)綁定到普通組件和自定義組件中,本文給大家介紹Vue3的v-model示例代碼,感興趣的朋友跟隨小編一起看看吧
    2023-02-02
  • Vue中對iframe實現(xiàn)keep alive無刷新的方法

    Vue中對iframe實現(xiàn)keep alive無刷新的方法

    這篇文章主要介紹了Vue中對iframe實現(xiàn)keep alive無刷新的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • electron-vue+electron-updater實現(xiàn)自動更新(步驟源碼)

    electron-vue+electron-updater實現(xiàn)自動更新(步驟源碼)

    這篇文章主要介紹了electron-vue+electron-updater實現(xiàn)自動更新,步驟源碼包括autoUpdater.js操控更新js文件,main.js也就是package.json內(nèi)的main指向的js文件,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10

最新評論

昌乐县| 韶关市| 明水县| 大新县| 息烽县| 大石桥市| 大新县| 祁连县| 日喀则市| 大同市| 醴陵市| 上高县| 蓝田县| 乐东| 沂水县| 赤峰市| 闻喜县| 台安县| 南汇区| 桂东县| 珠海市| 江陵县| 渝中区| 新野县| 京山县| 墨脱县| 咸丰县| 榕江县| 汤阴县| 鄂州市| 邢台市| 游戏| 任丘市| 宁晋县| 龙口市| 隆德县| 桓台县| 布尔津县| 禄丰县| 永吉县| 文登市|