在實際項目中,通常會遇到存在大量的業務條件選擇的場景,這種情況下如果使用大量的"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>
如果根據條件渲染不同的組件,可以使用 <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僅會改變元素的 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>
*請認真填寫需求信息,我們會在24小時內與您取得聯系。