Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
在實(shí)際項(xiàng)目中,通常會(huì)遇到存在大量的業(yè)務(wù)條件選擇的場(chǎng)景,這種情況下如果使用大量的"v-if"和"v-else"指令,會(huì)造成
1、頁(yè)面渲染性能下降,加載時(shí)間增加: 每個(gè)v-if 都需要遍歷并計(jì)算這些條件,尤其是在條件選擇復(fù)雜且計(jì)算開(kāi)銷(xiāo)較大時(shí),會(huì)導(dǎo)致初始渲染的耗時(shí)增加,從而延長(zhǎng)頁(yè)面的加載時(shí)間。
2、冗余代碼增加:過(guò)多的v-if 會(huì)導(dǎo)致模板代碼變得冗長(zhǎng)和難以維護(hù)。導(dǎo)致代碼可讀性降低,難以理解和調(diào)試。
3、可維護(hù)下降:當(dāng)模板中存在大量的v-if時(shí),由于每個(gè)條件判斷都是獨(dú)立的,修改其中一個(gè)條件可能需要修改多個(gè)地方,增加了出錯(cuò)的可能性,并使維護(hù)變得復(fù)雜。
4、內(nèi)存增加: 每個(gè)v-if條件都會(huì)生成對(duì)應(yīng)的DOM元素,并在切換條件時(shí)進(jìn)行創(chuàng)建和銷(xiāo)毀,當(dāng)模板中存在大量的v-if時(shí),會(huì)導(dǎo)致內(nèi)存占用增加,對(duì)性能和資源消耗產(chǎn)生影響。
將復(fù)雜的條件邏輯轉(zhuǎn)移到計(jì)算屬性中處理,避免在template模板中頻繁使用"v-if"和"v-else"。通過(guò)計(jì)算屬性的返回值來(lái)控制渲染的內(nèi)容, 這樣使得template代碼更簡(jiǎn)潔,條件處理的邏輯更清晰且更易維護(hù)。
<template>
<div>
<span v-if="displayText">{{ displayText }}</span>
</div>
</template>
<script>
export default {
data() {
return {
// ...
};
},
computed: {
displayText() {
// 在此處添加復(fù)雜的條件邏輯
if (/* condition */) {
return 'Text 1';
} else if (/* another condition */) {
return 'Text 2';
} else {
return 'Default Text';
}
},
},
};
</script>
如果根據(jù)條件渲染不同的組件,可以使用 <component :is="currentComponent"> 動(dòng)態(tài)切換組件。
這種優(yōu)化方式結(jié)合了工廠模式的使用,在工廠組件中注冊(cè)所有的component組件,根據(jù)傳入的 condition 知道具體生產(chǎn)哪個(gè)component,并使用 :is 進(jìn)行頁(yè)面渲染。
<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() {
// 在此處添加復(fù)雜的條件邏輯
if (/* condition */) {
return ComponentA;
} else if (/* another condition */) {
return ComponentB;
} else {
return ComponentC;
}
},
},
components: {
ComponentA,
ComponentB,
ComponentC,
},
};
</script>
當(dāng)需要頻繁切換元素的顯示和隱藏時(shí),可以使用v-show替代v-if。因?yàn)関-show僅會(huì)改變?cè)氐?CSS display屬性,避免了DOM元素頻繁切換顯示和隱藏,而v-if會(huì)將元素從 DOM 中完全移除或重新插入,但是v-show不支持<template>元素和v-else。
<template>
<div>
<span v-show="isVisible">顯示文本</span>
</div>
</template>
<script>
export default {
data() {
return {
isVisible: true,
};
},
};
</script>
將條件邏輯分解到更小的子組件中可以使得代碼更加模塊化和可維護(hù)。每個(gè)子組件可以負(fù)責(zé)處理自己的條件邏輯,從而降低父組件的復(fù)雜性。
<!-- 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>
如果某些條件在渲染過(guò)程中保持不變,可以在數(shù)據(jù)層面進(jìn)行預(yù)處理,并將結(jié)果緩存起來(lái)。這樣可以避免在模板中重復(fù)計(jì)算和判斷條件。
<template>
<div>
<template v-if="isConditionA">
<!-- 渲染條件 A 的內(nèi)容 -->
</template>
<template v-else-if="isConditionB">
<!-- 渲染條件 B 的內(nèi)容 -->
</template>
<template v-else>
<!-- 渲染默認(rèn)內(nèi)容 -->
</template>
</div>
</template>
<script>
export default {
data() {
return {
data: /* 原始數(shù)據(jù) */,
isConditionA: false,
isConditionB: false
};
},
created() {
// 預(yù)處理數(shù)據(jù),并計(jì)算條件結(jié)果
// 可以在這里對(duì) this.data 進(jìn)行處理,然后計(jì)算出 this.isConditionA 和 this.isConditionB 的值
}
}
</script>
作者:前端碎碎念
鏈接:https://juejin.cn/post/7254559214588575802
馳Xinchi外語(yǔ)
if...類省略句結(jié)構(gòu)用法
英語(yǔ)語(yǔ)法句法省略句
if-型省略結(jié)構(gòu)是一個(gè)非常有用的結(jié)構(gòu),本文擬對(duì)其可能涉及的結(jié)構(gòu)作一全面歸納。
一、if + 形容詞
這類結(jié)構(gòu)通常可視為在if與形容詞之間省略了“主語(yǔ)+動(dòng)詞be的適當(dāng)形式”。如:
Send the goods now if ready.=Send the goods now if they are ready. 貨物如已備好,請(qǐng)即送來(lái)。
If true, this will cause us a lot of trouble.=If it is true, this will cause us a lot of trouble. 這事若是事實(shí),它將給我們?cè)斐稍S多麻煩。
注:這類省略結(jié)構(gòu)中有的已構(gòu)成相對(duì)固定的搭配,if necessary (如果需要),if possible (如果可能)等。如:
If necessary, ring me at home. 如果必要,可往我家里打電話。
If possible, let me know beforehand. 如果可能,可在事前通知我。
二、if + 過(guò)去分詞
其中的過(guò)去分詞可視為是被省略的被動(dòng)結(jié)構(gòu),即在if與形容詞之間省略了主語(yǔ)和助動(dòng)詞be。如:
He will come if asked.=He will come if he is asked. 他如被邀就會(huì)來(lái)。
The medicine is quite effective if taken in time.=The medicine is quite effective if it is taken in time. 這藥要是能按時(shí)服用,效果是很好的。
三、if + 代詞
這類省略通常要根據(jù)具體的上下文來(lái)理解。如:
If anyone, he knows. 如果有人知道,那就是他了。
There are few people nowadays, if any, who remember him. 當(dāng)今記得他的人,如有的話,也不多了。
He seems to have little, if anything, to do with this. 若要說(shuō)他和這事有什么相關(guān)的話,那也似乎是很少的。
四、if + 介詞短語(yǔ)
這類結(jié)構(gòu)往往要根據(jù)具體的語(yǔ)境來(lái)理解,但有些經(jīng)常搭配的慣用結(jié)構(gòu)也值得注意,如if in doubt, if at all, if by any chance等。如:
If in doubt, ask your doctor. He can give you further information. 你若有疑問(wèn),可以問(wèn)問(wèn)醫(yī)生. 他會(huì)向你作進(jìn)一步的說(shuō)明。
Their policies have changed little, if at all, since the last election. 自上次選舉以來(lái),他們的政策就算是有所變化,也變得很少。
If by any chance you can't manage dinner tonight, perhaps we can at least have a drink together. 就算你今晚不吃晚飯,也許我們至少可以一起喝一杯。
五、if + ever
if ever 可視為習(xí)語(yǔ),它通常與seldom連用,表示“極少”“難得”。如:
She seldom, if ever, goes to the cinema. 她難得看電影。
He seldom if ever travels abroad. 他到國(guó)外旅行,即使有過(guò),也是極少的。
注:有時(shí)ever后面還修飾有其他詞語(yǔ)。如:
The island is seldom if ever visited by ships. 這個(gè)島難得有船??俊?/p>
另外,它有時(shí)還可引出一個(gè)句子。如:
If ever you're in Cambridge, do give me a ring. 萬(wàn)一你來(lái)劍橋,一定要給我打電話。
六、if + not
if not 可視為一個(gè)否定的條件狀語(yǔ)從句省略。如:
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? 問(wèn)問(wèn)她那個(gè)時(shí)間方便不方便。要是不方便,那她可不可以提出一個(gè)可行的時(shí)間???
注:有時(shí)not還可修飾另一個(gè)詞語(yǔ)。如:
If not today, tomorrow I'm sure you'll get an answer. 如果今天得不到回信,明天準(zhǔn)能得到This is one of the oldest buildings in town, if not the oldest. 這是城里最古老的房屋之一,如果不是最古老的話。
Usually, if not always, we write “cannot” as one word. 我們即使不總是如此,也通常是把cannot作為一個(gè)詞來(lái)拼寫(xiě)的。
七、if + so
if so的意思是“如果是那樣的話”。如:
I may be free this evening. If so, I'll come round and see you. 今晚我可能有空。要是有空我會(huì)過(guò)來(lái)看你。
They must decide if such a plan can be implemented and if so, when. 他必須決定這樣的計(jì)劃是否能實(shí)施,而且要是能實(shí)施的話,又得決定何時(shí)實(shí)施。
注意以下if so與if not連用的情形:
He may be busy. If so, I'll call later. If not, can I see him now? 他可能忙,如是這樣,我以后再來(lái)拜訪。他如不忙,我現(xiàn)在可以見(jiàn)他嗎?
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? 您要再往一晚嗎? 如果是這樣,我們可以給您提供條件更好一點(diǎn)的房間。如果不是,您能在12點(diǎn)前離開(kāi)這房間嗎?
八、if need be 如果需要
if need be為習(xí)語(yǔ),其含義相當(dāng)于if it is necessary (如果有必要的話)。如:
I will come if need be. 如有必要我會(huì)來(lái)。
I'll work at night if need be. 如果有必要我可以晚上工作。
If need be we can always bring another car. 如果有必要的話我們還可以再開(kāi)一輛車(chē)來(lái)。
可樂(lè)老師 編輯。4
轉(zhuǎn)發(fā)if...類省略句結(jié)構(gòu)用法_英語(yǔ)筆記_檸咚詞 https://www.ndsq.cn/ndsq_en_cn_blog/93007---.html 。
如有侵權(quán)請(qǐng)通知?jiǎng)h除。碼字不易,敬請(qǐng)【點(diǎn)贊】、【關(guān)注】!謝謝您的支持!
My email:ilikework_cz@126.com
作為Python Web 框架,Django 需要一種很便利的方法以動(dòng)態(tài)地生成HTML,最常見(jiàn)的做法是使用模板。模板包含所需HTML 輸出的靜態(tài)部分,以及一些特殊的語(yǔ)法,描述如何將動(dòng)態(tài)內(nèi)容插入。
Django 項(xiàng)目可以配置一個(gè)或多個(gè)模板引擎。Django 的模板系統(tǒng)自帶內(nèi)建的后臺(tái)-稱為Django 模板語(yǔ)言(DTL),以及另外一種流行的Jinja2。其他的模板語(yǔ)言的后端,可查找第三方庫(kù)。
在使用layui的時(shí)候,需要使用到layui數(shù)據(jù)表格的模板,這時(shí)候就遇到{{}}轉(zhuǎn)義的問(wèn)題。在django中{{}}是獲取變量值,這就跟前段的layui的模板沖突了,這時(shí)候就需要django不轉(zhuǎn)譯指定的內(nèi)容。
<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'}">編號(hào)</th>
<th lay-data="{field:'name', sort: true,width: '180'}">任務(wù)名稱</th>
<th lay-data="{field:'task_type' , sort: true,width: 140}">任務(wù)類型</th>
<th lay-data="{field:'task_custom_parameter' ,sort: true,width: '200'}">自定義參數(shù)</th>
<th lay-data="{field:'username' ,sort: true,width: '120'}">創(chuàng)建者</th>
<th lay-data="{field:'status_label' ,sort: true,width: '100'}">執(zhí)行狀態(tài)</th>
<th lay-data="{field:'create_time' ,sort: true,width: '190'}">創(chuàng)建時(shí)間</th>
<th lay-data="{field:'exec_time' ,sort: true,width: '190'}">執(zhí)行時(shí)間</th>
<th lay-data="{field:'detail_result' ,sort: true,width: '200'}">執(zhí)行結(jié)果</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">已執(zhí)行</button>
{{# } else if(d.status=='N') { }}
<a class="layui-btn layui-btn-xs" lay-event="exec">執(zhí)行</a>
{{# } else if(d.status=='R') { }}
<span class="layui-badge layui-bg-orange layui-btn-xs">執(zhí)行中</span>
{{# } }}
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
</script>
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。