Pug文件编译返回mixins.pug is not found的错误

Sur*_*j M 2 html gruntjs pug

我是哈巴狗/玉的新手。我有 3 个 pug 模板文件,名为“layout.pug”、“home.pug”和“mixins.pug”。layout.pug 包含以下代码:

html
  include mixins
  body
    include home
Run Code Online (Sandbox Code Playgroud)

home.pug 文件中的代码:

body
  div.main
    +popup('Hello', 'Hello World')
Run Code Online (Sandbox Code Playgroud)

pug 文件“mixins.pug”包含 mixins。我已将 mixin popup() 添加到此文件中。该文件中的代码:

mixin popup(title, description)
 div.pop-up-body
   h2 #{title}
   p #{description}
Run Code Online (Sandbox Code Playgroud)

但是当我使用grunt pug命令编译 pug 文件时,出现错误“pug_mixins.popup 不是函数”。

如果您知道原因,请帮助我。

Sur*_*j M 5

我得到了这个问题的答案。

错误原因: 我已将其包含在文件mixins.puglayout.pug。但是 mixinpopup(title, description)是从home.pug文件中调用的。所以该 mixin 在该文件中不可用home.pug。(它home.pug也包含在layout.pug文件中。所以我mixins.pug仅将其包含在layout.pug文件中)。

解决方案:include mixins我从文件中 删除了代码layout.pug并将其添加到home.pug文件中。

代码: layout.pug文件

html
   body
      include home
Run Code Online (Sandbox Code Playgroud)

home.pug 文件

include mixins
div.main
   +popup('Hello', 'Hello World') 
Run Code Online (Sandbox Code Playgroud)

mixins.pug 文件

mixin popup(title, description)
   div.pop-up-body
       h2 #{title}
       p #{description}
Run Code Online (Sandbox Code Playgroud)