技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
vk-custom-component【数据与研究】Use when developing custom components for vk-unicloud-admin framework's universal table and form system. Especially when: (1) Creating new custom components with `custom-` prefix, (2) Implementing components that work across form/table/detail scenarios, (3) Understanding component props (value, column, scene), events (input, change), and lifecycle re…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
custom- prefix, (2) Implementing components that work across form/table/detail scenarios, (3) Understanding component props (value, column, scene), events (input, change), and lifecycle requirements. Covers component structure, naming conventions, scenario-specific implementations, and integration with vk-data-form and vk-data-table."
license: MIT本 skill 提供 vk-unicloud-admin 框架自定义组件开发的完整指导。vk-unicloud-admin 框架的万能表单和万能表格支持通过自定义组件扩展功能,开发者可以创建满足特定业务需求的组件。
自定义组件支持三种使用场景:
在项目根目录的 components 目录下创建组件文件,文件名必须使用 custom-组件名 格式:
components/
custom-aaa/
custom-aaa.vue
<template>
<!-- 万能表单 -->
<view v-if="scene === 'form'">
<el-input :value="value" @input="_input"></el-input>
</view>
<!-- 万能表格 -->
<view v-else-if="scene === 'table'">
<text>{{ value }}</text>
</view>
<!-- 万能表格详情 -->
<view v-else-if="scene === 'detail'">
<text>{{ value }}</text>
</view>
</template>
<script>
export default {
props: {
// 双向绑定的值
value: {
type: [String, Number, Boolean, Object, Array],
default: ""
},
// 字段规则
column: {
type: Object,
default: function() {
return {};
}
},
// 当前场景:form/table/detail
scene: {
type: String,
default: "form"
}
},
data() {
return {};
},
mounted() {},
methods: {
_input(value) {
// 固定顺序:先 input,再 change
this.$emit("input", value);
this.$emit("change", value);
}
},
watch: {},
computed: {}
};
</script>
<style lang="scss" scoped>
</style>
在 vk-data-form 或 vk-data-table 的 columns 配置中使用:
{
key: "key1",
title: "我是自定义组件",
type: "custom",
component: "custom-aaa"
}
组件目录和文件必须使用 custom-组件名 格式:
components/custom-aaa/custom-aaa.vuecustom-aaa组件需要支持三种使用场景:
form(万能表单)
input 和 change 事件input,再 changedisabled、readonly 等状态table(万能表格)
value 值进行渲染detail(表格详情)
value 值进行渲染自定义组件接收以下 props:
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
value | Any | "" | 双向绑定的值 |
column | Object | {} | 字段规则配置对象 |
scene | String | "form" | 当前场景(form/table/detail) |
自定义组件需要触发以下事件:
| Event | 参数 | 描述 | 触发时机 |
|---|---|---|---|
input | value | 表单双向绑定必需事件 | 用户修改输入值时 |
change | value | 值变化事件 | 用户修改输入值时 |
重要:事件触发顺序必须是先 input,再 change。
在 components 目录下创建组件目录和文件:
components/
custom-组件名/
custom-组件名.vue
根据三种场景实现组件模板:
<template>
<!-- form 场景 -->
<view v-if="scene === 'form'">
<!-- 表单输入组件 -->
</view>
<!-- table 场景 -->
<view v-else-if="scene === 'table'">
<!-- 表格显示组件 -->
</view>
<!-- detail 场景 -->
<view v-else-if="scene === 'detail'">
<!-- 详情显示组件 -->
</view>
</template>
form 场景需要:
methods: {
_input(value) {
this.$emit("input", value);
this.$emit("change", value);
}
}
table 和 detail 场景需要:
<view v-else-if="scene === 'table'">
<text>{{ value }}</text>
</view>
在表单或表格中测试组件:
在 vk-data-form 或 vk-data-table 的 columns 配置中使用组件:
// 表单配置
form1: {
props: {
columns: [
{
key: "field_name",
title: "字段标题",
type: "custom",
component: "custom-组件名"
}
]
}
}
// 表格配置
table1: {
columns: [
{
key: "field_name",
title: "列标题",
type: "custom",
component: "custom-组件名",
width: 150
}
]
}
form 场景用于万能表单,需要支持用户输入和数据绑定。
要求:
input 事件change 事件input,再 changedisabled、readonly 等状态示例:
<view v-if="scene === 'form'">
<el-input
:value="value"
:disabled="column.disabled"
:readonly="column.readonly"
:placeholder="column.placeholder"
@input="_input"
></el-input>
</view>
table 场景用于万能表格的单元格,用于展示数据。
要求:
value 值进行渲染示例:
<view v-else-if="scene === 'table'">
<text>{{ value }}</text>
</view>
detail 场景用于表格详情页,可以展示更详细的信息。
要求:
value 值进行渲染示例:
<view v-else-if="scene === 'detail'">
<text>{{ value }}</text>
</view>
必须先触发 input 事件,再触发 change 事件:
// ✅ 正确
methods: {
_input(value) {
this.$emit("input", value);
this.$emit("change", value);
}
}
// ❌ 错误
methods: {
_input(value) {
this.$emit("change", value);
this.$emit("input", value);
}
}
组件目录和文件必须使用 custom-组件名 格式:
✅ 正确:custom-aaa/custom-aaa.vue
❌ 错误:aaa/aaa.vue
❌ 错误:custom-aaa/aaa.vue
正确定义 props 的类型,支持多种数据类型:
props: {
value: {
type: [String, Number, Boolean, Object, Array],
default: ""
}
}
使用 scoped 限制样式作用域:
<style lang="scss" scoped>
.custom-component {
// 组件样式
}
</style>
正确处理异步操作和错误情况:
async loadRemoteData() {
this.loading = true;
try {
const res = await vk.callFunction({
url: this.column.action,
data: {}
});
if (res.code === 0) {
this.options = res.list;
} else {
vk.toast(res.msg || "加载失败");
}
} catch (error) {
console.error("加载失败:", error);
vk.toast("加载失败");
} finally {
this.loading = false;
}
}
使用计算属性、防抖节流等技术优化性能:
computed: {
displayValue() {
if (!this.value) return "";
if (this.column.formatter) {
return this.column.formatter(this.value);
}
return this.value;
}
}
完整的 Props、Events、Column 对象属性说明,参见 API 参考。
包含以下组件的完整实现代码:
参见 完整示例。
包含以下内容:
参见 高级用法。