1

Parrent component

<progress-bar 
  :maxQuote = "maxQuotes" 
  :currentQuote="allQuotes.length" >
</progress-bar>

data: function() {
  return {
    allQuotes: [],
    maxQuotes: 10
  };
},

Progressbar Component

<template>
  <div class="container">
    <div class="progress">
      <div class="progress-bar" :style="{'width': +90 + '%'}">{{current}} / {{max}}</div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["maxQuote", "currentQuote"],
  data: function() {
    return {
      max: this.maxQuote,
      current: this.currentQuote
    };
  }
};
</script>

Here I want to pass the length of my allQuotes[] array maxQuote prop passed successfully but currentQuote not passed any number , even after array values are increased !

2
  • Can you add how you defined the props in progress-bar component? Commented Jul 18, 2020 at 9:48
  • Here i updated my post , plese check Commented Jul 18, 2020 at 9:53

1 Answer 1

1

You are passing props, but then you assign them to reactive data() and you use those in your template. What happens, is that your props instantiate the data() props, but then they are not changing them anymore when the props change. You should just use the props inside your child component, like so:

<template>
  <div class="container">
    <div class="progress">
      <div class="progress-bar" :style="{'width': +90 + '%'}">{{currentQuote}} / {{maxQuote}}</div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["maxQuote", "currentQuote"],
  data: function() {
    return {
   
    };
  }
};
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks bro , but I have a question is props are another kind of data() ?
Say yes. The huge difference is that props are coming from a parent component (usually from the data of the parent , like in your example) and you can't change them anywhere else that from where they are passed (in the parent). Attempting to change them in the child will cause an error, cause you are breaking the flow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.