javascript - 遍历vuejs中本地注册的组件

标签 javascript vue.js

我有一个 Vue 组件,可以在本地导入和注册其他组件。我想遍历组件对象并动态呈现它们。我正在尝试通过以下方式实现这一点(在 .vue 文件中):

<template>
  <div v-for="component in components" class="component">
    <component v-bind:is="component"></component>
  </div>
</template>

<script>
import CarouselDefault from './carousel/CarouselDefault'
import AlertError from './alerts/AlertError'
import AlertInfo from './alerts/AlertInfo'
import AlertSuccess from './alerts/AlertSuccess'
import AlertWarning from './alerts/AlertWarning'

export default {
  name: 'App',
  components: {
    CarouselDefault,
    AlertError,
    AlertInfo,
    AlertSuccess,
    AlertWarning
  }
}
</script>

我收到了这个错误信息:

Property or method "components" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

最佳答案

components 属性在 vue 的模板构建器中不可用,您必须定义 computed 或 data 的属性。 示例:

computed:{
components(){
       return [
          CarouselDefault,
          AlertError,
          AlertInfo,
          AlertSuccess,
          AlertWarning
       ];
}
}

data(){
return {
 components:[
    CarouselDefault,
    AlertError,
    AlertInfo,
    AlertSuccess,
    AlertWarning
  ]
}
}

关于javascript - 遍历vuejs中本地注册的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53911687/

相关文章:

javascript - jCarousel 自动滚动不起作用,没有错误

javascript - jQuery/js 可以帮助我确定 URL 是否存在

javascript - 将 QuaggaJs 的宽度设置为 100%?

javascript - Nuxt js静态页面相对URL

javascript - 如何修复 javascript 没有分配正确的值?

javascript - 如何插入带有 contenteditable 元素的新页面

javascript - pdf已经下载后可以使用PDF.js吗?

typescript - Pinia:无法从 Nuxt3 API 访问商店

vue.js - v槽:badge and v-if does not work with a computed property

javascript - 如何在 nuxt.config.js 中扩展 webpack > config > externals?