我有一个组件列表,我想从外部为它们设置配置,

例如:

 const myConfig = [
  {
    name: 'example',
    renderer: () => (<button @click="clickHanlder">Click me!</button>)
  },
  ...
 ];

对于我的组件,我想使用myConfig,如下所示:

  <template>
    <div class="example">
      <template v-for="(item, index) in myConfig" :key="index">
        My Button:
        <div class="example-2">{{ item.renderer() }}</div>
      </template>
    </div>
  </template>

请注意,我不想用slot

我该怎么做?

推荐答案

您可以通过使用h渲染功能来实现这一点,如以下示例所示:

<template v-for="(item, index) in myConfig" :key="index">
    My Button:
    <div class="example-2">
        <component :is="item.renderer" />
    </div>
</template>
<script setup lang="ts">

import {  h } from 'vue';

const myConfig = [
  {
    name: 'example',
    renderer: h('button', {
  
      onClick:clickHanlder
      
    },'Click me !'),
  },

 ];

 function clickHanlder(){
    console.log('click btn');
 }

</script>

Vue.js相关问答推荐

如何用其他组件编译Vue模板?

Vue3组合-如何在组件卸载时注销回调

在 Vuejs 中更新 Chartjs 图表数据集

Vuetify 3 - NavBar 组件占据整个视口高度

如何解决 importers[path] is not a function for clean Vue 3/Vite/Storybook installation

VueJS CKeditor5 上传图片

如何使用 vue 或 javascript 向服务器发送密码?

使用 vuex 更新数据

正确实现 Vue.js + DataTables

Vuetify / Offline离线图标

[Vue 警告]:无效的props:propsscrollThreshold的类型判断失败.期望的数字,得到字符串

基本 vue.js 2 和 vue-resource http 获取变量赋值

使用props向组件添加类名

如何告诉 Vue-cli 我的应用程序的入口点在哪里?

使用 Vue 在单个文件组件中调用渲染方法

Vue 2 转换不起作用

仅在提交时验证 vuetify 文本字段

如果通过 jquery 更新,Vuejs 绑定不起作用

返回 VueJS 方法的Native Code消息

Vue-meta:metaInfo 无权访问计算(computed)属性