Home Web Front-end JS Tutorial How to solve the problem that the vuex method in the parent component updates the state and the child component cannot be updated and rendered in time

How to solve the problem that the vuex method in the parent component updates the state and the child component cannot be updated and rendered in time

Jun 29, 2018 pm 04:11 PM
vuex Subassembly

This article mainly introduces the perfect solution for the vuex method in the parent component to update the state subcomponent that cannot be updated and rendered in time. Friends in need can refer to the following

Scenario:

What I actually use is this. My parent component refers to the child component related. The parent component calls the method to get the page details, updates the state value related, and the child component renders related based on the related. News content, but the subcomponent is always loaded first when the page is opened. The subcomponent has not obtained the updated related value when rendering. Even if the change of the value is watched in the subcomponent, the relevant news of the subcomponent cannot be rendered. content.

My solution:

The parent component passes the value to the child component. When the parent component executes the method of getting page details After that, the state value related is updated and then passed to the subcomponent, which is then rendered and can be obtained normally.

Parent component code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

<template>

 <p id="newsDetails">

  <mt-header title="详情">

   <router-link to="/" slot="left">

    <mt-button icon="back"></mt-button>

   </router-link>

  </mt-header>

  <p class="details clearfloat">

   <h1 class="titleFont">

    {{ title }}

   </h1>

   <p 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=&#39;btnFollow&#39; @click="follow">关注</span>

   </p>

   <p class="bodyFont clearfloat" id="bodyFont" ref="bodyFont" :class="{bodyHeight:contentStatus}">

    <p v-html="content"></p>

    <p class="editor" v-if="editorName">责任编辑:{{editorName}}</p>

   </p>

   <p class="contentToggle" @click="contentStatus=!contentStatus" v-if="contentStatus">阅读全文</p>

   <Related :related="related"></Related>

    <!--重点是这里 父组件向子组件传值-->

 </p> </p> </template>

 

import { Toast } from &#39;mint-ui&#39;;

 import {mapState} from &#39;vuex&#39;

 import Related from &#39;./Related.vue&#39;

 import moment from &#39;moment&#39;;

 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(&#39;zh-cn&#39;);

  },

  mounted(){

   setTimeout(()=>{

    this.contentToggle();

   },500)

  },

  watch: {

   &#39;$route&#39;(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(&#39;getDetails&#39;,this.id);

   },

   follow(){

    Toast(&#39;登录后进行关注&#39;);

    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);

   },

  }

 }

Copy after login

Child component related.vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

<template>

  <p v-if="lists.length>0">

    <p class="tagTitle"><span>相关新闻</span></p>

    <p class="listItem" v-if="(item.type==&#39;little&#39;)" v-for="(item,index) in lists" :to="{name:&#39;details&#39;,params:{id:item.id}}" :key="index" @click="browserDetection()">

     <p class="listImg1">

      <!--<img :src="{lazy==loaded?item.thumb[0]:lazy==loading?&#39;../../assets/images/little_loading.png&#39;:lazy==error?&#39;../../assets/images/little_loading.png&#39;}" alt="" v-lazy="item.thumb[0]">-->

      <img :src="item.thumb[0]" alt="" v-lazy="item.thumb[0]">

     </p>

     <p class=&#39;titleBox1&#39;>

      <p class="listTitle">{{item.title}}</p>

      <p 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>

      </p>

    </p>

   </p>

  </p>

</template>

<script>

 import {mapActions, mapState, mapGetters} from &#39;vuex&#39;

 import moment from &#39;moment&#39;

 export default{

  data(){

   return {

    lists: [],

    id:this.$route.params.id,

   }

  },

  props:{

    related:Array  //重点是这里

  },

  created(){

   moment.locale(&#39;zh-cn&#39;);

  },

  /*computed: {

   ...mapState({

    related: state => state.newsDetails.related,

   })

  },*/

  filters:{

   formatTime(time){

    return moment(time).fromNow();

   },

  },

  methods:{

  },

  watch: {

   related (val) {

    this.lists = val;

   },

   &#39;$route&#39;(to,from){

    this.id=this.$route.params.id

   }

  }

 }

</script>

Copy after login

The effect is as shown in the figure:

The above is the entire content of this article, I hope it will be helpful to everyone’s learning For help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

How vue achieves the effect of forward refresh and backward without refresh

About the implementation of Vue comment framework ( Implementation of parent component)

The above is the detailed content of How to solve the problem that the vuex method in the parent component updates the state and the child component cannot be updated and rendered in time. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to solve the problem 'Error: [vuex] unknown action type: xxx' when using vuex in a Vue application? How to solve the problem 'Error: [vuex] unknown action type: xxx' when using vuex in a Vue application? Jun 25, 2023 pm 12:09 PM

In Vue.js projects, vuex is a very useful state management tool. It helps us share state among multiple components and provides a reliable way to manage state changes. But when using vuex, sometimes you will encounter the error "Error:[vuex]unknownactiontype:xxx". This article will explain the cause and solution of this error. 1. Cause of the error When using vuex, we need to define some actions and mu

How to solve the problem 'Error: [vuex] do not mutate vuex store state outside mutation handlers.' when using vuex in a Vue application? How to solve the problem 'Error: [vuex] do not mutate vuex store state outside mutation handlers.' when using vuex in a Vue application? Jun 24, 2023 pm 07:04 PM

In Vue applications, using vuex is a common state management method. However, when using vuex, we may sometimes encounter such an error message: "Error:[vuex]donotmutatevuexstorestateoutsidemutationhandlers." What does this error message mean? Why does this error message appear? How to fix this error? This article will cover this issue in detail. The error message contains

Best practices for using Vuex to manage global state in Vue2.x Best practices for using Vuex to manage global state in Vue2.x Jun 09, 2023 pm 04:07 PM

Vue2.x is one of the most popular front-end frameworks currently, which provides Vuex as a solution for managing global state. Using Vuex can make state management clearer and easier to maintain. The best practices of Vuex will be introduced below to help developers better use Vuex and improve code quality. 1. Use modular organization state. Vuex uses a single state tree to manage all the states of the application, extracting the state from the components, making state management clearer and easier to understand. In applications with a lot of state, modules must be used

How to use Vuex in Vue3 How to use Vuex in Vue3 May 14, 2023 pm 08:28 PM

What does Vuex do? Vue official: State management tool What is state management? State that needs to be shared among multiple components, and it is responsive, one change, all changes. For example, some globally used status information: user login status, user name, geographical location information, items in the shopping cart, etc. At this time, we need such a tool for global status management, and Vuex is such a tool. Single-page state management View–>Actions—>State view layer (view) triggers an action (action) to change the state (state) and responds back to the view layer (view) vuex (Vue3.

Learn more about the implementation principles of vuex Learn more about the implementation principles of vuex Mar 20, 2023 pm 06:14 PM

When asked in an interview about the implementation principle of vuex, how should you answer? The following article will give you an in-depth understanding of the implementation principle of vuex. I hope it will be helpful to you!

How to solve the problem 'TypeError: Cannot read property 'xxx' of undefined' when using vuex in Vue application? How to solve the problem 'TypeError: Cannot read property 'xxx' of undefined' when using vuex in Vue application? Aug 18, 2023 pm 09:24 PM

Using Vuex in Vue applications is a very common operation. However, occasionally when using Vuex, you will encounter the error message "TypeError: Cannotreadproperty'xxx'ofundefined". This error message means that the undefined property "xxx" cannot be read, resulting in a program error. The reason for this problem is actually very obvious. It is because when calling a certain attribute of Vuex, this attribute is not correctly set.

How to use vuex in vue3+vite How to use vuex in vue3+vite Jun 03, 2023 am 09:10 AM

Specific steps: 1. Install vuex (vue3 recommended 4.0+) pnpmivuex-S2, configure the global configuration of importstorefrom'@/store'//hx-app in main.js constapp=createApp(App)app.use(store) 3. Create new related folders and files. Here, configure multiple js inside different vuex. Use vuex modules to place different pages and files, and then use a getters.jsindex.js core file. Import.meta.glob is used here. , instead of

Let's talk about the two Vue state management libraries Pinia and Vuex. Which one should I use? Let's talk about the two Vue state management libraries Pinia and Vuex. Which one should I use? Feb 15, 2023 pm 03:08 PM

This article will talk about Vue state management and introduce two Vue state management libraries: Pinia and Vuex. I hope it will be helpful to you!

See all articles