父組件中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)站的支持!
- vue中v-model的應(yīng)用及使用詳解
- vue項(xiàng)目中v-model父子組件通信的實(shí)現(xiàn)詳解
- vue 自定義組件 v-model雙向綁定、 父子組件同步通信的多種寫法
- 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)文章
使用canvas實(shí)現(xiàn)一個(gè)vue彈幕組件功能
這篇文章主要介紹了使用canvas實(shí)現(xiàn)一個(gè)vue彈幕組件功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
Vue?通過this.$emit()方法子組件向父組件傳值(步驟分享)
這篇文章主要介紹了Vue?this.$emit()方法通過子組件向父組件傳值,第一步在父組件中引入子組件,第二步子組件向父組件傳值,本文通過需要的朋友可以參考下2022-11-11
Vue中對iframe實(shí)現(xiàn)keep alive無刷新的方法
這篇文章主要介紹了Vue中對iframe實(shí)現(xiàn)keep alive無刷新的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
electron-vue+electron-updater實(shí)現(xiàn)自動(dòng)更新(步驟源碼)
這篇文章主要介紹了electron-vue+electron-updater實(shí)現(xiàn)自動(dòng)更新,步驟源碼包括autoUpdater.js操控更新js文件,main.js也就是package.json內(nèi)的main指向的js文件,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10

