First, in the actual development stage , Should not put v-if and v-for Use... In the same label ,

stay vue2 in ,v-for Priority is higher than v-if Of , If it appears at the same time , Each rendering will execute a loop first and then judge the conditions , In any case, the cycle is inevitable , Wasted performance ; Another thing to note is that vue3 On the contrary ,v-if Has a higher priority than v-for, therefore v-if Execution time , The variable it calls doesn't exist yet , It leads to an exception .

We usually do this in two cases

  • To filter the items in the list ( such as :v-for="user in users" v-if="user.isActive"). At this point, define a calculation attribute ( such as :activeUsers), Let it return to the filtered list ( such as :users.filter(u=>u.isActive)
  • To avoid rendering lists that should have been hidden ( such as v-for="user in users" v-if="shouldShowUsers"), At this time v-if Move to container element ( such as ulol) Or it's wrapped in a layer template that will do .
 <div id="app">
<p v-for="child in children" v-if="isFolder">{{child.title}}</p>
</div>
<script>
const vm=new Vue({
el:'#app',
data(){
return{
children:[
{
title:'red'
},
{
title:'green'
}
]
}
},
computed:{
isFolder(){
return this.children&&this.children.length>0
}
}
})
console.log(vm.$options.render);

The rendering functions are as follows :

ƒ anonymous(
) {
with(this){return _c('div',{attrs:{"id":"app"}},_l((children),function(child){return (isFolder)?_c('p',[_v(_s(child.title))]):_e()}))}
}

Optimized code :

<template v-if="isFolder">
<p v-for="child in children">{{child.title}}</p>
</template>

The rendering functions are as follows :

ƒ anonymous(
) {
with(this){return _c('div',{attrs:{"id":"app"}},[(isFolder)?_l((children),function(child){return _c('p',[_v(_s(child.title))])}):_e()],2)}
}

v-if and v-for Which is the higher priority ? More articles about

  1. SAP WM TO Print Control In the set ,Movement Type Higher priority

    SAP WM TO Print Control In the set ,Movement Type Higher priority Configuration of storage type : from storage type GRM Move anywhere , No need to print TO single . Mobile type configuration ...

  2. python—— Than return Higher priority statements

    call sqlmap, Use sqlmap When doing secondary development , What happened : Calling sqlmap in return, However, the main program will still be sqlmap Some of the code in interrupt . add to try There's no way to stop interruptions . Later it was speculated that the interruption was caused by ex ...

  3. poj 2762 Going from u to v or from v to u?

    Title Description : To make their son braver ,Jiajia and Wind Take them to a big cave . There's a hole in the cave n A room , There are some one-way channels connecting certain rooms . Every time ,Wind Choose two rooms x and y, Ask one of their sons to walk from a room to ...

  4. turn :oracle Several important sets of common views -v$undostat,v$open_cursor,v$rowcache,v$session_longops,v$waitstat

    v$undostat This view monitors... In the current instance undo Space and how transactions work . And statistics undo Space overhead , Transaction cost and query length available for instance . V$UNDOSTAT Common columns in Endtime: With 10 Minutes is the end of the interval ...

  5. turn :oracle Several important sets of common views -v$segstat,v$segment_statistics,v$filestat,v$rollstat

    v$segstat This view monitors segment level in real time (segment-level) Statistical items , Support oracle9ir2 And higher V$SEGSTAT Common columns in TS#: Table space identifier OBJ#: Dictionary object id DATAOBJ ...

  6. turn :oracle Common important views -v$sql,v$sql_plan,v$sqltext,v$sqlarea,v$sql_plan_statistcs

    v$sql V$SQL Store specific SQL sentence . A statement can map multiple cursor, Because the object refers to cursor There can be different users ( For example 1). If there are more than one cursor( Child cursors ) There is , stay V$SQLAREA For all c ...

  7. turn :Oracle Several important sets of views in --v$sysstat,v$system_event,v$parameter v$system_parameter

    Several important performance views by component 1.System Of over view v$sysstat , v$system_event  , v$parameter,V$instance obtain oracle_sid ...

  8. poj 2762 Going from u to v or from v to u?( Strong connectivity 、 Shrinkage point 、 Topology )

    The question :( You're wrong ) There are many in a cave room, It is required to select any two room:u.v, Can guarantee u.v There is a path between , Note that the path in the cave has a directional edge .. analysis : The points in a strongly connected subgraph must be interconnected , There is a path between two strongly connected subgraphs ...

  9. Oracle Basic data dictionary :v$database、v$instance、v$version、dba_objects

    v$database: View structure : SQL> desc v$database; Name                                      Null?    Type - ...

  10. POJ2762 Going from u to v or from v to u( Simply connected Shrinkage point )

    Determine whether the graph is simply connected , First, deal with the strongly connected partition graph , Then topological sorting , Attention should be paid to : What meets the requirements is not necessarily the unique result of the chain topology arrangement , That is, there is always only one element in the queue #include<cstdio> #include< ...

Random recommendation

  1. Conditional comments judge browser version &lt;!--[if lt IE 9]&gt;

    <!--[if !IE]><!--> except IE All can be recognized <!--<![endif]--><!--[if IE]> be-all IE Recognizable <![e ...

  2. Python Basics - obtain N The day before

    Get the... Before today N The date of day from datetime import datetime from datetime import timedelta def get_date(days=N): retur ...

  3. SQL Server Batch data export and import BCP Use

    BCP brief introduction bcp yes SQL Server A command line tool responsible for importing and exporting data in , It is based on DB-Library Of , And can efficiently import and export a large number of data in a parallel way .bcp You can export the tables or views of the database directly , Can also pass through ...

  4. Floyd Algorithm application - The problem of hospital location

    1) Problem description n The traffic map between two villages can be represented by directed network graph , Edge in the picture <vi, vj> The weight on represents from the village i To the village j The length of the road . Now we need to start from here n Choose one of the villages to build a new hospital , Which village should the hospital be built in ...

  5. Android About China JNI Learning from ( 6、 ... and )JNI Implementation of registration method in

    In the previous example , We will find that , When in Java When defining a method in a class , For example, the following : public class ParamTransferTest { public static int testval = 1 ...

  6. One day CSS: Support IE Of Layout Layout --16

    1.BFC and Layout difference : BFC and Layout It's the same thing , It's just that the browser support is different . BFC- - Standard browsers have Layout- -IE The browser has BFC Detailed address :http://b ...

  7. 【Python3 Reptiles 】 Public opinion crawler ( Crack CSS Anti creeping )

    The target of this crawler is the name of some shops in the public comments . Recommended dishes and rating information . One . Page analysis Enter the public comments , And then choose food (http://www.dianping.com/wuhan/ch10), You can see it on one page ...

  8. C# Control the left and right channels

    private void button1_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.settings.balance = -; ...

  9. MVC Source code analysis - Action/Result Filter execution timing

    front Chapter , Parsed Action Method search , as well as Authorize, Action, Result, Error Loading time of filter . I also spent two articles on the use of authorization and error filters . But for Actio ...

  10. Linux command dd And cp The difference between

    Link to the original text :http://blog.csdn.net/erazy0/article/details/6087554 ask : Read some about dd and cp The order of , But I still can't understand dd and cp What's the difference between ? Not all ...