Tips for using prop in computed properties
P粉893457026
P粉893457026 2023-08-25 13:19:04
0
1
548
<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粉893457026
P粉893457026

reply all(1)
P粉790819727

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
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template