【问题标题】:The pug mixin doesn't return the expected content哈巴狗混合不返回预期的内容
【发布时间】:2020-12-24 05:10:45
【问题描述】:

我是哈巴狗的新手,有一个问题。我们在 Vue.js 应用程序中使用 pug,我想生成多级菜单(带有子菜单)。我们使用如下数据:

mounted () {
  this.catalog = [
    {
      title: "Компрессоры",
      permalink: "kompressory",
      hasChildren: true,
      cover: "kk",
      subcatalog: [
        {
          title: "A1",
          permalink: "a1",
          hasChildren: false,
          cover: "aa1",
        },
        {
          title: "B1",
          permalink: "b1",
          hasChildren: false,
          cover: "bb1",
        }
      ]
    },
    {
      title: "Ручной инструмент",
      permalink: "ruchnoy-instrument",
      hasChildren: true,
      cover: "rr",
      subcatalog: [
        {
          title: "C1",
          permalink: "c1",
          hasChildren: false,
          cover: "cc1",
        },
        {
          title: "D1",
          permalink: "d1",
          hasChildren: false,
          cover: "dd1",
        },
        {
          title: "E1",
          permalink: "e1",
          hasChildren: false,
          cover: "ee1",
        }
      ]
    },
    {
      title: "Пневмоинструмент",
      permalink: "pnevmoinstrument",
      hasChildren: false,
      cover: "pp",
    },
    {
      title: "Специальный инструмент",
      permalink: "spetsialnyy-instrument",
      hasChildren: false,
      cover: "ss",
    },
    {
      title: "Оборудование для СТО",
      permalink: "garazhnoe-oborudovanie",
      hasChildren: false,
      cover: "oo",
    },
  ]
}

它应该是垂直菜单,顶部有一个按钮。单击按钮时,带有子菜单的菜单会下拉。我们使用 Quasar 的 QMenu 作为菜单组件。

工作模板代码如下:

<template lang="pug">
  .q-pa-md
    .q-gutter-md.row.items-center

      q-btn(color='primary', label='Click me')
        q-menu(v-model='showing')
          q-list(dense='dense', style='min-width: 100px')
            q-item(v-for='category in catalog', :key='category.permalink', href='#', clickable='clickable', v-close-popup='v-close-popup')
              q-item-section {{ category.title }}
              q-item-section(side='side')
                q-icon(name='keyboard_arrow_right', v-if='category.hasChildren')
              q-menu(anchor='top end', self='top start')
                q-list
                  q-item(v-for='n in 3', :key='n', dense='dense', clickable='clickable')
                    q-item-section Submenu Label
                    q-item-section(side='side')
                      q-icon(name='keyboard_arrow_right')
                    q-menu(auto-close='auto-close', anchor='top end', self='top start')
                      q-list
                        q-item(v-for='n in 3', :key='n', dense='dense', clickable='clickable')
                          q-item-section 3rd level Label
            q-separator
            q-item(clickable='clickable', v-close-popup='v-close-popup')
              q-item-section Quit
</template>

截图:

但是,我编写了如下所示的 createMenu.pug mixin:

mixin createMenu(catalog)
  q-list(dense style='min-width: 100px')
    q-item(v-for='category in catalog', :key='category.permalink', href='#', clickable='clickable', v-close-popup='v-close-popup')
      q-item-section {{ category.title }}
      q-item-section(side='side')
        q-icon(name='keyboard_arrow_right', v-if='category.hasChildren')

如您所见,我刚刚将部分代码从主模板移到了 mixin 中,仅此而已。

但是,当我尝试以这种方式在主模板中使用该 mixin 时:

<template lang="pug">
  include ./pug_mixins/createMenu.pug

  .q-pa-md
    .q-gutter-md.row.items-center
      q-btn(color='primary', label='Click me')
        q-menu(v-model='showing')
          +createMenu(catalog)
            q-menu(anchor='top end', self='top start')
              q-list
                q-item(v-for='n in 3', :key='n', dense='dense', clickable='clickable')
                  q-item-section Submenu Label
                  q-item-section(side='side')
                    q-icon(name='keyboard_arrow_right')
                  q-menu(auto-close='auto-close', anchor='top end', self='top start')
                    q-list
                      q-item(v-for='n in 3', :key='n', dense='dense', clickable='clickable')
                        q-item-section 3rd level Label
          q-separator
          q-item(clickable='clickable', v-close-popup='v-close-popup')
            q-item-section Quit
</template>

我无法打开子菜单。

截图:

我不确定我做错了什么。如何解决这个问题?

【问题讨论】:

    标签: vue.js pug quasar-framework


    【解决方案1】:

    你需要告诉 mixin 声明在哪里渲染传递给它的块内容。您可以通过在 mixin 声明的适当部分添加 block 来做到这一点。在您的情况下,需要将声明修改为:

    mixin createMenu(catalog)
      q-list(dense style='min-width: 100px')
        q-item(v-for='category in catalog', :key='category.permalink', href='#', clickable='clickable', v-close-popup='v-close-popup')
          q-item-section {{ category.title }}
          q-item-section(side='side')
            q-icon(name='keyboard_arrow_right', v-if='category.hasChildren')
          if block
            block
    

    有关详细信息,请参阅 Pug 文档中的 Mixin Blocks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多