简体   繁体   English

玉-对象和混合

[英]Jade - objects and mixins

So I am trying to pass an object to a mixin and I have my mixin as follows: 所以我试图将一个对象传递给一个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}

I call the mixin as follows: 我称mixin如下:

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

It works fine when I call with all the variables but if I dont pass a certain variable in the object then it throws an error saying its undefined. 当我调用所有变量时,它工作正常,但是如果我没有在对象中传递某个变量,那么它将抛出错误,指出其未定义。 For example for the second mixin call above I get the error Cannot read property 'dropdown' of undefined because its not defined. 例如,对于上面的第二个mixin调用,我收到错误消息Cannot read property 'dropdown' of undefined因为未定义。

How do I properly check if they are defined and avoid errors? 如何正确检查它们是否已定义并避免错误?

It is good practice to check if the values are available inside the function or mixin before using them. 在使用它们之前,最好检查一下这些值是否在函数或mixin中可用。 You can use conditional statement to avoid such problems. 您可以使用条件语句来避免此类问题。 You can do following for example: 您可以例如执行以下操作:

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