父組件中vuex方法更新state子組件不能及時(shí)更新并渲染的完美解決方法
場(chǎng)景:
我實(shí)際用到的是這樣的,我父組件引用子組件related,父組件調(diào)用獲取頁(yè)面詳情的方法,更新了state值related,子組件根據(jù)該related來(lái)渲染相關(guān)新聞內(nèi)容,但是頁(yè)面打開(kāi)的時(shí)候總是先加載子組件,子組件在渲染的時(shí)候還沒(méi)有獲取到更新之后的related值,即使在子組件中watch該值的變化依然不能渲染出來(lái)子組件的相關(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">
<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">打開(kāi)唐人家</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í)更新并渲染的完美解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue中v-model的應(yīng)用及使用詳解
- vue項(xiàng)目中v-model父子組件通信的實(shí)現(xiàn)詳解
- vue 自定義組件 v-model雙向綁定、 父子組件同步通信的多種寫(xiě)法
- Vue 進(jìn)階教程之v-model詳解
- 利用Vue v-model實(shí)現(xiàn)一個(gè)自定義的表單組件
- vue 2.0組件與v-model詳解
- Vue2.0利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案
- vue.js指令v-model實(shí)現(xiàn)方法
- vuex state及mapState的基礎(chǔ)用法詳解
- 詳解Vuex中mapState的具體用法
- Vuex 使用 v-model 配合 state的方法
相關(guān)文章
解決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)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Vue3監(jiān)聽(tīng)reactive對(duì)象中屬性變化的方法
在 Vue 3 中,如果你想監(jiān)聽(tīng) reactive 對(duì)象中的某個(gè)屬性發(fā)生的變化,你可以使用 watch 函數(shù)進(jìn)行監(jiān)聽(tīng),watch 函數(shù)允許你觀察 reactive 對(duì)象的某個(gè)屬性或者整個(gè)對(duì)象,所以本文給大家介紹了Vue3監(jiān)聽(tīng)reactive對(duì)象中屬性變化的方法,需要的朋友可以參考下2024-08-08
記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn)
這篇文章主要介紹了記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Vue使用視頻作為網(wǎng)頁(yè)背景的實(shí)現(xiàn)指南
在現(xiàn)代網(wǎng)頁(yè)設(shè)計(jì)中,視頻背景逐漸成為一種流行的設(shè)計(jì)趨勢(shì),它不僅能夠提升網(wǎng)頁(yè)的動(dòng)態(tài)效果,還可以在視覺(jué)上抓住用戶(hù)的注意力,本文將詳細(xì)講解如何在頁(yè)面中使用視頻作為背景,并確保內(nèi)容可見(jiàn)、頁(yè)面元素布局合理,需要的朋友可以參考下2024-10-10
前端Vue3項(xiàng)目打包成Docker鏡像運(yùn)行的詳細(xì)步驟
將Vue3項(xiàng)目打包、編寫(xiě)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)仿百度下拉列表示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
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í)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

