A, should not use
v-for v-if and should not be used together.
The reason: v-for
more than v-if
a priority, that is, every time need to traverse the entire array, the impact speed.
For example the old code encountered when the reconstruction project:
<div
v-for="(fileMsg,index) in fileMsgList"
:key="fileMsg.id"
v-if="index < 2"
>
<sys-file-layout :fileMsg="fileMsg"></sys-file-layout>
</div>
Want to loop generates a series of component blocks, but do not want to generate content after the serial number of 1, simultaneously with the v-if
and v-for
, then, or will generate all the components of the block according to the entire array, only after judge v-if
to allow excess hours, very resource intensive.
Second, the solution
1, if necessary, should be replaced with a computed property.
computed: {
fileMsgListCom() {
return this.fileMsgList.filter((item, index) => {
return item < 2;
});
}
}
<div
class="file_name"
v-for="(fileMsg,index) in fileMsgListCom"
:key="fileMsg.id"
>
<sys-file-layout :fileMsg="fileMsg"></sys-file-layout>
</div>
2, the v-if
change v-show
.
If this v-for
upper layer has been v-for
circulating, and here just to take an array of objects in the upper cycle continues for recycling (in fact, the project encountered here is the problem), I can not think computed using the method to calculate how to calculate the property (get less than this array contents multilayer cycle).
Can also be v-if
changed v-show
, you can coexist.
<div
class="file_name"
v-for="(fileMsg,index) in file.documents"
:key="fileMsg.id"
v-show="index < 2"
>
<sys-file-layout :fileMsg="fileMsg"></sys-file-layout>
</div>