微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何创建pugjs组件?

如何解决如何创建pugjs组件?

是否有可能创建 pugjs 组件,我可以在其中调用该组件,例如 x-button Sign In

组件看起来像这样

button(class="py-3 px-8 rounded-lg inline-flex justify-center items-center font-semibold text-base leading-6 tracking-wide") #{text}

类似于这个概念Laravel component

解决方法

这就是 Pug mixins 的用途:

// declare the mixin
mixin x-button
  button.py-3.px-8.rounded-lg.inline-flex.justify-center.items-center.font-semibold.text-base.leading-6.tracking-wide
    if block
      block

// use the mixin
+x-button Sign In
,

组件的 Pug 等价物是 template inheritanceincludes。包含适用于您的用例:

//- includes/button.pug
button(class="py-3 px-8 rounded-lg inline-flex justify-center items-center font-semibold text-base leading-6 tracking-wide") #{text}
//- some-page.pug
//- index.pug
doctype html
html
  head
  body
    - var text = 'some-button-text'
    include includes/button.pug

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。