简体   繁体   English

如何在 Vue 中使用模块系统导入动态 v-bind:is 组件

[英]How to import dynamic v-bind:is component with module system in Vue

Problem: I am trying to programmatically register a component to be used in a slot in my Vue/Nuxt site.问题:我正在尝试以编程方式注册要在我的 Vue/Nuxt 站点的插槽中使用的组件。 The component name is included in the data of the parent index.vue file, in this instance the component is named Projects .组件名称包含在父index.vue文件的数据中,在这种情况下,组件名为Projects I am including it in a v-for template as the various objects in the 'modules' data array are iterated over.我将它包含在v-for模板中,因为'modules'数据数组中的各种对象都被迭代了。 I had assumed this would be possible/easy from the dynamic component documentation and example however I have not managed to get it working in my case.我曾假设从动态组件文档示例中这是可能的/容易的,但是我没有设法让它在我的情况下工作。

What I expect to happen: I expected the component to be registered and 'slotted' into the Module component correctly.我期望发生的事情:我期望组件被正确注册并“插入”到Module组件中。

What actually happens: While I can see on the rendered view that the component is 'there', it is not in the correct place (ie slotted into the Module component).实际发生的情况:虽然我可以在渲染视图中看到该组件“存在”,但它不在正确的位置(即插入到Module组件中)。 I also get a vue/no-unused-components error saying: The "Projects" component has been registered but not used .我还收到一个vue/no-unused-components错误,提示: The "Projects" component has been registered but not used

I have read the documentation about component registration in modular systems but these seem to be for more complex cases than what I am trying to achieve.我已经阅读了有关模块化系统中组件注册的文档,但这些似乎是针对比我想要实现的更复杂的情况。 Any advice would be really helpful as I am totally stuck!任何建议都会非常有帮助,因为我完全被卡住了!

index.vue

<template>
  <div>
    <template v-for="module in modules">
      <Module
        :title="module.title"
        :to="module.link"
      />
      <component v-bind:is="module.slot" />
      </Module>
    </template>
  </div>
</template>

<script>
import Module from '~/components/module/Module.vue'
import Projects from '~/components/module/slots/Projects.vue'
export default {
  components: {
    Module,
    Projects
  },
  data () {
    return {
      modules: [
        {
          title: 'Work',
          slot: 'Projects'
        },
        {
          ...
        }
      ]
    }
  }
}
</script>

Edit: As a side note, I get the same error when registering the component with import like so:编辑:作为旁注,我在使用import注册组件时遇到同样的错误,如下所示:

components: {
  Module,
  'Projects': () => import('@/components/module/slots/Projects')
}

Module.vue

<template>
  <div>
    <h2>
      {{ title }}
    </h2>
    <slot />
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: ''
    }
  }
}
</script>

Projects.vue

<template>
  <div>
    <h3>Projects</h3>
  </div>
</template>
<script>
export default {
  name: 'Projects'
}
</script>

You use the self closure tag in your Module component.您在Module组件中使用自闭包标签。

This prevents your Projects component to be rendered within the slot.这可以防止您的Projects组件在插槽内呈现。

Just replace:只需更换:

<Module
   :title="module.title"
    :to="module.link"
/>

with:和:

<Module
   :title="module.title"
    :to="module.link"
>

and it should work.它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM