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