Home  >  Q&A  >  body text

Tips for using prop in computed properties

<p>I have a carousel component that receives a prop called 'mediaItems', which I use in a computed property to determine the end of the carousel: </p> <pre class="brush:php;toolbar:false;">props: ['mediaItems', 'sliderHeading'], computed: { atEndOfList() { return this.currentOffset <= (this.paginationFactor * -1) * (this.mediaItems.length / this.windowSize) this.paginationFactor; },</pre> <p>This results in the component being empty and I get a console error: </p> <blockquote> <p>Type error: this.mediaItems is undefined</p> </blockquote> <p>If I remove the computed property, the component loads the props and no console error occurs, but I need this computed property to determine the end of the carousel. </p>
P粉893457026P粉893457026420 days ago531

reply all(1)I'll reply

  • P粉790819727

    P粉7908197272023-08-26 12:26:33

    I think you should specify a default value for this property to make computed properties work properly when the mediaItems property has not been set externally:

    props: {
      mediaItems: {
        type: Array,
        default: ()=>[]
      }, 
      sliderHeading: String
    }

    reply
    0
  • Cancelreply