把 Vue 业务组件放进图库
已有设备详情按钮、读数卡或趋势组件时,给它声明可编辑的属性,就能复用到多张画面。假设要把一个「查看设备」按钮交给设计人员,允许他们在画布上改按钮文字。
准备组件
新建 PlantButton.vue。组件接收 text 字符串,用父容器大小确定自身尺寸。
vue
<script setup lang="ts">
defineProps<{ text: string }>();
</script>
<template>
<button type="button" style="width:100%;height:100%">{{ text }}</button>
</template>在宿主和图库中注册
在 main.ts 挂载应用前注册组件,再把同名 ID 的定义加入图库。以下示例用一个内联图标作为图库缩略图。
ts
import { createApp } from 'vue';
import { leftAsideStore } from 'maotu';
import App from './App.vue';
import PlantButton from './PlantButton.vue';
const app = createApp(App);
app.component('plant-button', PlantButton);
const icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 32"><rect x="2" y="4" width="76" height="24" rx="5" fill="#16776d"/></svg>';
leftAsideStore.registerConfig('业务组件', [{
id: 'plant-button', title: '设备详情按钮', type: 'vue',
thumbnail: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(icon),
width: 120, height: 40,
props: { text: { title: '按钮文字', type: 'input', val: '查看设备' } }
}]);
app.mount('#app');
这是演示库的「我的按钮」。自定义按钮的文字同样由宿主组件接收,右侧字段名来自注册时填写的属性标题。
检查属性能否传进组件
在「业务组件」搜索“设备详情按钮”,拖入画布,把「按钮文字」改成“查看水泵”。画布和预览都应出现新文字。某个实例上的修改只影响该实例。若要在预览里点击按钮后打开业务台账,继续将点击事件交给宿主。
如果编辑器和预览分属两个应用,两边都要注册 plant-button 及图库定义。画面 JSON 保存组件标识和配置,组件代码由宿主提供。