您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“父組件中vuex方法更新state子組件不能及時(shí)更新并渲染怎么辦”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“父組件中vuex方法更新state子組件不能及時(shí)更新并渲染怎么辦”這篇文章吧。
場(chǎng)景:
我實(shí)際用到的是這樣的,我父組件引用子組件related,父組件調(diào)用獲取頁(yè)面詳情的方法,更新了state值related,子組件根據(jù)該related來渲染相關(guān)新聞內(nèi)容,但是頁(yè)面打開的時(shí)候總是先加載子組件,子組件在渲染的時(shí)候還沒有獲取到更新之后的related值,即使在子組件中watch該值的變化依然不能渲染出來子組件的相關(guān)新聞內(nèi)容。
我的解決辦法:
父組件像子組件傳值,當(dāng)父組件執(zhí)行了獲取頁(yè)面詳情的方法之后,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"> <h2 class="titleFont"> {{ title }} </h2> <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'}" v-lazy="item.thumb[0]">--> <img :src="item.thumb[0]" 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>
效果如圖:
以上是“父組件中vuex方法更新state子組件不能及時(shí)更新并渲染怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。