首页 > 解决方案 > 将带有变量的哈巴狗标记存储到变量中(不重复)

问题描述

我使用模板文字(模板字符串)在 pug mixin 中复制了代码

   input(class=`${concatenatedClasses}`, type=`${type}`, placeholder=`${placeholder}`)

代码如下所示:

  if modificator === 'masked'
    .text-field__wrapper-masked
      input(class=`${concatenatedClasses}`, type=`${type}`, placeholder=`${placeholder}`)
  else if modificator === 'email'
    -placeholder = 'Email'
    -type = 'email'
    .text-field__wrapper-subscription
      span.material-icons
        | arrow_forward
      input(class=`${concatenatedClasses}`, type=`${type}`, placeholder=`${placeholder}`)
  else
    input(class=`${concatenatedClasses}`, type=`${type}`, placeholder=`${placeholder}`)

我想将重复的代码存储在一个变量中,所以我不必重复。像这样:

  -let textFieldOutput = input(class=`${concatenatedClasses}`, type=`${type}`, placeholder=`${placeholder}`)

  if modificator === 'masked'
    .text-field__wrapper-masked
      #{textFieldOutput}
  else if modificator === 'email'
    -placeholder = 'Email'
    -type = 'email'
    .text-field__wrapper-subscription
      span.material-icons
        | arrow_forward
      #{textFieldOutput}
  else
    #{textFieldOutput}

但是当我尝试将 pug 标记存储在变量中时会导致语法错误。

  - Error: Error parsing body of the with expression

有没有办法做到这一点?

非常感谢您的关注和帮助。

标签: syntaxpugmarkup

解决方案


推荐阅读