整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

如何优化Vue template 中的大量条件选择v

如何优化Vue template 中的大量条件选择v-if

量v-if的弊端

在实际项目中,通常会遇到存在大量的业务条件选择的场景,这种情况下如果使用大量的"v-if"和"v-else"指令,会造成

1、页面渲染性能下降,加载时间增加: 每个v-if 都需要遍历并计算这些条件,尤其是在条件选择复杂且计算开销较大时,会导致初始渲染的耗时增加,从而延长页面的加载时间。

2、冗余代码增加:过多的v-if 会导致模板代码变得冗长和难以维护。导致代码可读性降低,难以理解和调试。

3、可维护下降:当模板中存在大量的v-if时,由于每个条件判断都是独立的,修改其中一个条件可能需要修改多个地方,增加了出错的可能性,并使维护变得复杂。

4、内存增加: 每个v-if条件都会生成对应的DOM元素,并在切换条件时进行创建和销毁,当模板中存在大量的v-if时,会导致内存占用增加,对性能和资源消耗产生影响。

可选的优化方案

利用计算属性

将复杂的条件逻辑转移到计算属性中处理,避免在template模板中频繁使用"v-if"和"v-else"。通过计算属性的返回值来控制渲染的内容, 这样使得template代码更简洁,条件处理的逻辑更清晰且更易维护。

<template> 
    <div> 
        <span v-if="displayText">{{ displayText }}</span> 
    </div> 
 </template> 
 <script> 
     export default { 
         data() { 
             return { 
                 // ... 
              }; 
         }, 
         computed: { 
             displayText() { 
                 // 在此处添加复杂的条件逻辑 
                     if (/* condition */) { 
                        return 'Text 1'; 
                      } else if (/* another condition */) { 
                        return 'Text 2'; 
                      } else { 
                        return 'Default Text'; 
                       } 
               }, 
         },
     }; 
 </script>


使用异步动态组件(Dynamic components)

如果根据条件渲染不同的组件,可以使用 <component :is="currentComponent"> 动态切换组件。

这种优化方式结合了工厂模式的使用,在工厂组件中注册所有的component组件,根据传入的 condition 知道具体生产哪个component,并使用 :is 进行页面渲染。

<template> 
    <div> 
        <component :is="currentComponent"></component> 
    </div> 
</template> 
<script> 
    import ComponentA from './ComponentA.vue'; 
    import ComponentB from './ComponentB.vue'; 
    import ComponentC from './ComponentC.vue'; 
    export default { 
        data() { 
            return { 
                // ... 
             }; 
         }, 
         computed: { 
             currentComponent() { 
                // 在此处添加复杂的条件逻辑 
                if (/* condition */) { 
                     return ComponentA; 
                 } else if (/* another condition */) { 
                     return ComponentB; 
                 } else { 
                     return ComponentC; 
                 } 
             }, 
        }, 
        components: { 
            ComponentA, 
            ComponentB, 
            ComponentC, 
         }, 
    }; 
  </script>


使用v-show代替v-if

当需要频繁切换元素的显示和隐藏时,可以使用v-show替代v-if。因为v-show仅会改变元素的 CSS display属性,避免了DOM元素频繁切换显示和隐藏,而v-if会将元素从 DOM 中完全移除或重新插入,但是v-show不支持<template>元素和v-else。

<template>
    <div> 
        <span v-show="isVisible">显示文本</span> 
    </div> 
</template> 
<script> 
    export default { 
        data() { 
            return { 
                isVisible: true, 
             }; 
         }, 
    }; 
</script>


将条件逻辑移入子组件

将条件逻辑分解到更小的子组件中可以使得代码更加模块化和可维护。每个子组件可以负责处理自己的条件逻辑,从而降低父组件的复杂性。

<!-- ParentComponent.vue --> 
<template> 
    <div> 
        <child-component :data="data"></child-component> 
    </div> 
</template> 
<script> 
    import ChildComponent from './ChildComponent.vue'; 
    export default { 
        components: { 
            ChildComponent, 
        }, 
        data() { 
            return { 
                data: /* some data */, 
             }; 
         }, 
    }; 
</script>   
<!-- ChildComponent.vue --> 
<template> 
    <div> 
        <span v-if="condition1">Text 1</span> 
        <span v-else-if="condition2">Text 2</span> 
        <span v-else>Default Text</span> 
    </div> 
</template> 
<script> 
    export default { 
        props: ['data'], 
        computed: { 
            condition1() { 
                // Calculate condition1 based on this.data 
            }, 
            condition2() { 
                // Calculate condition2 based on this.data 
            }, 
        }, 
    }; 
</script>


数据预处理

如果某些条件在渲染过程中保持不变,可以在数据层面进行预处理,并将结果缓存起来。这样可以避免在模板中重复计算和判断条件。

<template>
  <div>
    <template v-if="isConditionA">
      <!-- 渲染条件 A 的内容 -->
    </template>
    <template v-else-if="isConditionB">
      <!-- 渲染条件 B 的内容 -->
    </template>
    <template v-else>
      <!-- 渲染默认内容 -->
    </template>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: /* 原始数据 */,
      isConditionA: false,
      isConditionB: false
    };
  },
  created() {
    // 预处理数据,并计算条件结果
    // 可以在这里对 this.data 进行处理,然后计算出 this.isConditionA 和 this.isConditionB 的值
  }
}
</script>



作者:前端碎碎念
链接:https://juejin.cn/post/7254559214588575802

驰Xinchi外语

if...类省略句结构用法

英语语法句法省略句

if-型省略结构是一个非常有用的结构,本文拟对其可能涉及的结构作一全面归纳。

一、if + 形容词

这类结构通常可视为在if与形容词之间省略了“主语+动词be的适当形式”。如:

Send the goods now if ready.=Send the goods now if they are ready. 货物如已备好,请即送来。

If true, this will cause us a lot of trouble.=If it is true, this will cause us a lot of trouble. 这事若是事实,它将给我们造成许多麻烦。

注:这类省略结构中有的已构成相对固定的搭配,if necessary (如果需要),if possible (如果可能)等。如:

If necessary, ring me at home. 如果必要,可往我家里打电话。

If possible, let me know beforehand. 如果可能,可在事前通知我。

二、if + 过去分词

其中的过去分词可视为是被省略的被动结构,即在if与形容词之间省略了主语和助动词be。如:

He will come if asked.=He will come if he is asked. 他如被邀就会来。

The medicine is quite effective if taken in time.=The medicine is quite effective if it is taken in time. 这药要是能按时服用,效果是很好的。

三、if + 代词

这类省略通常要根据具体的上下文来理解。如:

If anyone, he knows. 如果有人知道,那就是他了。

There are few people nowadays, if any, who remember him. 当今记得他的人,如有的话,也不多了。

He seems to have little, if anything, to do with this. 若要说他和这事有什么相关的话,那也似乎是很少的。

四、if + 介词短语

这类结构往往要根据具体的语境来理解,但有些经常搭配的惯用结构也值得注意,如if in doubt, if at all, if by any chance等。如:

If in doubt, ask your doctor. He can give you further information. 你若有疑问,可以问问医生. 他会向你作进一步的说明。

Their policies have changed little, if at all, since the last election. 自上次选举以来,他们的政策就算是有所变化,也变得很少。

If by any chance you can't manage dinner tonight, perhaps we can at least have a drink together. 就算你今晚不吃晚饭,也许我们至少可以一起喝一杯。

五、if + ever

if ever 可视为习语,它通常与seldom连用,表示“极少”“难得”。如:

She seldom, if ever, goes to the cinema. 她难得看电影。

He seldom if ever travels abroad. 他到国外旅行,即使有过,也是极少的。

注:有时ever后面还修饰有其他词语。如:

The island is seldom if ever visited by ships. 这个岛难得有船停靠。

另外,它有时还可引出一个句子。如:

If ever you're in Cambridge, do give me a ring. 万一你来剑桥,一定要给我打电话。

六、if + not

if not 可视为一个否定的条件状语从句省略。如:

I might see you tomorrow. If not, then it'll be Saturday. 我可能明天去看你。如果不是明天,那就在周六。

Ask her if it is a convenient time. If not, can she suggest another possible time? 问问她那个时间方便不方便。要是不方便,那她可不可以提出一个可行的时间???

注:有时not还可修饰另一个词语。如:

If not today, tomorrow I'm sure you'll get an answer. 如果今天得不到回信,明天准能得到This is one of the oldest buildings in town, if not the oldest. 这是城里最古老的房屋之一,如果不是最古老的话。

Usually, if not always, we write “cannot” as one word. 我们即使不总是如此,也通常是把cannot作为一个词来拼写的。

七、if + so

if so的意思是“如果是那样的话”。如:

I may be free this evening. If so, I'll come round and see you. 今晚我可能有空。要是有空我会过来看你。

They must decide if such a plan can be implemented and if so, when. 他必须决定这样的计划是否能实施,而且要是能实施的话,又得决定何时实施。

注意以下if so与if not连用的情形:

He may be busy. If so, I'll call later. If not, can I see him now? 他可能忙,如是这样,我以后再来拜访。他如不忙,我现在可以见他吗?

Will you be staying another night? If so, we can give you a better room. If not, could you be out of your room by 12:00? 您要再往一晚吗? 如果是这样,我们可以给您提供条件更好一点的房间。如果不是,您能在12点前离开这房间吗?

八、if need be 如果需要

if need be为习语,其含义相当于if it is necessary (如果有必要的话)。如:

I will come if need be. 如有必要我会来。

I'll work at night if need be. 如果有必要我可以晚上工作。

If need be we can always bring another car. 如果有必要的话我们还可以再开一辆车来。

可乐老师 编辑。4

转发if...类省略句结构用法_英语笔记_柠咚词 https://www.ndsq.cn/ndsq_en_cn_blog/93007---.html 。

如有侵权请通知删除。码字不易,敬请【点赞】、【关注】!谢谢您的支持!

My email:ilikework_cz@126.com

作为Python Web 框架,Django 需要一种很便利的方法以动态地生成HTML,最常见的做法是使用模板。模板包含所需HTML 输出的静态部分,以及一些特殊的语法,描述如何将动态内容插入。

Django 项目可以配置一个或多个模板引擎。Django 的模板系统自带内建的后台-称为Django 模板语言(DTL),以及另外一种流行的Jinja2。其他的模板语言的后端,可查找第三方库。


问题

在使用layui的时候,需要使用到layui数据表格的模板,这时候就遇到{{}}转义的问题。在django中{{}}是获取变量值,这就跟前段的layui的模板冲突了,这时候就需要django不转译指定的内容。

<table class="layui-table" lay-data="{width: 'auto', height:'auto', url:'/auto_tasks/task_view/', page:true, id:'autotaskviews'}"

lay-filter="autotaskviews_table" lay-size="xm">

<thead>

<tr>

<th lay-data="{field:'id',sort: true, fixed: true,width:'80'}">编号</th>

<th lay-data="{field:'name', sort: true,width: '180'}">任务名称</th>

<th lay-data="{field:'task_type' , sort: true,width: 140}">任务类型</th>

<th lay-data="{field:'task_custom_parameter' ,sort: true,width: '200'}">自定义参数</th>

<th lay-data="{field:'username' ,sort: true,width: '120'}">创建者</th>

<th lay-data="{field:'status_label' ,sort: true,width: '100'}">执行状态</th>

<th lay-data="{field:'create_time' ,sort: true,width: '190'}">创建时间</th>

<th lay-data="{field:'exec_time' ,sort: true,width: '190'}">执行时间</th>

<th lay-data="{field:'detail_result' ,sort: true,width: '200'}">执行结果</th>

<th lay-data="{fixed: 'right', align:'center',width: '180', toolbar: '#barDemo' }">查看详情</th>

</tr>

</thead>

</table>

<script type="text/html" id="barDemo"> {{# if(d.status=='Y'){ }}

<button class="layui-btn layui-btn-disabled layui-btn-xs">已执行</button>

{{# } else if(d.status=='N') { }}

<a class="layui-btn layui-btn-xs" lay-event="exec">执行</a>

{{# } else if(d.status=='R') { }}

<span class="layui-badge layui-bg-orange layui-btn-xs">执行中</span>

{{# } }}

<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>

</script>