javascript - 如何在 Vue 中使用模块系统导入动态 v-bind:is 组件
问题描述
问题:我正在尝试以编程方式注册要在我的 Vue/Nuxt 站点的插槽中使用的组件。组件名称包含在父index.vue
文件的数据中,在这种情况下,组件名为Projects
。我将它包含在v-for
模板中,因为'modules'
迭代数据数组中的各种对象。我曾假设从动态组件文档和示例中这是可能的/容易的,但是我没有设法让它在我的情况下工作。
我期望发生的事情:我期望组件被正确注册并“插入”到Module
组件中。
实际发生的情况:虽然我可以在渲染视图上看到组件“存在”,但它不在正确的位置(即插入Module
组件中)。我也收到一条vue/no-unused-components
错误消息:The "Projects" component has been registered but not used
.
我已经阅读了有关模块化系统中组件注册的文档,但这些似乎是针对比我想要实现的更复杂的情况。任何建议都会非常有帮助,因为我完全被卡住了!
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>
编辑:作为旁注,我在注册组件时遇到同样的错误,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>
解决方案
您在Module
组件中使用自闭标签。
这可以防止您的Projects
组件在插槽内呈现。
只需更换:
<Module
:title="module.title"
:to="module.link"
/>
和:
<Module
:title="module.title"
:to="module.link"
>
它应该可以工作。
推荐阅读
- java - 为什么通过 Java 代码生成的报告是空的?我在 Studio 中使用 jdbc dataadapter 得到了有效结果
- excel - 使用 VBA for Excel,如果列包含任何文本,则复制一行
- java - 如何在java中实现Goal Seek excel函数?apache poi 或库中是否有任何函数为 java 提供此功能
- android - 检测片段何时再次获得焦点
- git - git commit -m "add !empty check" zsh: event not found: empty
- c# - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) 返回不正确的文件夹路径
- javascript - 计算值为 A 的复选框总数
- apache-spark - 用涉及复杂数据类型的嵌套模式替换火花数据框中的空值
- typescript - 类型的最佳方法 - Typescript 前端 TCL 后端
- apache-spark - 如何合并火花数据框中的两行以在输出中获取空值