组件配置
组件配置是一个 JSON 格式文件,用于描述组件的元信息、属性、事件、方法等信息,编辑器会根据属性声明 来显示和配置组件。
组件配置语法规范
组件配置的 JSON Schema 可以查看 微搭组件声明语法规范
配置示例
下面以一个按钮组件为例,演示组件配置的格式
src/configs/components/button.json
{
"$schema": "https://comp-public-1303824488.cos.ap-shanghai.myqcloud.com/schema/lcds_component.json",
"data": {
"type": "object",
"properties": {
"text": {
"title": "按钮文字",
"default": "按钮",
"type": "string"
},
"size": {
"title": "按钮大小",
"type": "string",
"default": "default",
"x-component": "radio",
"enum": [
{
"label": "default",
"value": "default"
},
{
"label": "mini",
"value": "mini"
}
]
},
"type": {
"title": "按钮类型",
"type": "string",
"default": "primary",
"x-component": "radio",
"enum": [
{
"label": "default",
"value": "default"
},
{
"label": "primary",
"value": "primary"
},
{
"label": "warn",
"value": "warn"
}
]
},
"loading": {
"title": "加载中",
"type": "boolean",
"default": false
},
"disabled": {
"title": "禁用",
"type": "boolean",
"default": false
},
"plain": {
"title": "镂空",
"type": "boolean",
"default": false
},
"titleSlot": {
"type": "slot"
}
}
},
"events": [{ "name": "customevent", "title": "自定义事件" }],
"methods": [{ "name": "innermethod", "label": "组件内部方法" }],
"defaultStyles": {
"width": "400px",
"height": "100px"
},
"meta": {
"title": "按钮",
"description": "按钮组件,兼容小程序和H5平台",
"icon": "./icon.svg",
"category": "表单",
"componentOrder": 1
}
}
下面逐一对 JSON 中的属性配置进行说明
data 属性声明
选填,组件可以接收的数据,用作组件入参,需要符合 JSON Schema 规范。
编辑器也会根据该配置,绘制组件的数据面板。
示例
{
"data": {
"properties": {
"string": {
"title": "字符串",
"type": "string",
"default": "这是属性值",
"description": "这是属性在编辑器中的描述信息",
"required": true,
"x-index": 1
},
"enumString": {
"title": "enum下拉选择器",
"enum": [
{
"label": "默认",
"value": "default"
},
{
"label": "警告",
"value": "warn"
}
],
"type": "string",
"x-index": 13
},
"contentSlot": {
"title": "插槽",
"type": "slot",
"x-index": 6
},
"customRadio": {
"title": "自定义单选框",
"type": "string",
"x-component": "radio",
"enum": [
{
"label": "option1",
"value": "1"
},
{
"label": "option2",
"value": "2"
}
],
"x-index": 7
}
}
}
}