簡體   English   中英

玉-對象和混合

[英]Jade - objects and mixins

所以我試圖將一個對象傳遞給一個mixin,我的mixin如下:

mixin summarySection(panelID, bodyID, title, options)
    div(class='panel panel-default panel-summary')
        div(id=panelID class='panel-heading' role='tab')
            h4(class='panel-title')= title
            div(class='pull-right')
                if(options.dropdown)
                    select
                        option #{options.dropdown.options}
                a(role='button' data-toggle='collapse' href='##{bodyID}' aria-expanded='#{options.expanded}' aria-controls=bodyID) +
        div(id=bodyID class='panel-collapse collapse' role='tabpanel' aria-labelledby=panelID class= options.hasOwnProperty('expanded') ? 'in' : '')
            div(class='panel-body')
                if block
                    block
                else
                    p Content goes here
            if (options.footer)
                div(class='panel-footer text-center')
                    a(href='#{options.footer.link}') #{options.footer.text}

我稱mixin如下:

+summarySection('panelRecentActivity', 'bodyRecentActivity', 'Recent Activity', {'expanded': 'true', 'dropdown': {'options': 'Last 30 days'}})
+summarySection('panelStatements', 'bodyStatements', 'Statements')

當我調用所有變量時,它工作正常,但是如果我沒有在對象中傳遞某個變量,那么它將拋出錯誤,指出其未定義。 例如,對於上面的第二個mixin調用,我收到錯誤消息Cannot read property 'dropdown' of undefined因為未定義。

如何正確檢查它們是否已定義並避免錯誤?

在使用它們之前,最好檢查一下這些值是否在函數或mixin中可用。 您可以使用條件語句來避免此類問題。 您可以例如執行以下操作:

if options && options.footer
   a(href='#{options.footer.link}') #{options.footer.text}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM