相關(guān)文檔鏈接:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#%E5%A4%96%E9%83%A8%E6%A0%B7%E5%BC%8F%E7%B1%BB
通過文檔可知,小程序自定義組件有名為externalClasses的屬性,可以以數(shù)組字符串的方法引入父組件的樣式
示例方法如下:
子組件
/* 組件 custom-component.js */
Component({
externalClasses: ['my-class']
})
<!-- 組件 custom-component.wxml -->
<custom-component class="my-class">這段文本的顏色由組件外的 class 決定</custom-component>
父組件
<!-- 頁(yè)面的 WXML -->
<custom-component my-class="red-text" />
/* 頁(yè)面的 WXSS */
.red-text {
color: red;
}
注:此處有個(gè)坑點(diǎn),externalClasses是一個(gè)字符串?dāng)?shù)組,但我再具體應(yīng)用過程中發(fā)現(xiàn),里面的字符串命名方式對(duì)駝峰命名不友好,如上面的“my-class”寫成“myClass”,在實(shí)際應(yīng)用中會(huì)失效
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。