Appearance
- default
- chenghua
- shijingshan
- sybz
- gulou
无数据
Appearance
sybz-components 仓库里常见且推荐复用的写法。packages/components/foo/
├─ index.ts
└─ src/index.vue
docs/components/foo/
├─ home.md
├─ base.vue
└─ usually.vuepackages/components/foo/
├─ index.ts
└─ src/index.vue
docs/components/foo/
├─ home.md
├─ base.vue
└─ usually.vuepackages/components/*。packages/components/company/*。docs/components/*,一个组件一个目录。src/index.vue <script setup lang="ts">
import { computed, ref, useAttrs, useSlots, watch } from 'vue'
defineOptions({
name: 'SFoo',
})
const attrs = useAttrs()
const slots = useSlots()
const emits = defineEmits(['change', 'update:modelValue'])
interface FooProps {
modelValue?: string | number | any[]
width?: string | number
}
const props = withDefaults(defineProps<FooProps>(), {
modelValue: undefined,
width: '',
})
</script><script setup lang="ts">
import { computed, ref, useAttrs, useSlots, watch } from 'vue'
defineOptions({
name: 'SFoo',
})
const attrs = useAttrs()
const slots = useSlots()
const emits = defineEmits(['change', 'update:modelValue'])
interface FooProps {
modelValue?: string | number | any[]
width?: string | number
}
const props = withDefaults(defineProps<FooProps>(), {
modelValue: undefined,
width: '',
})
</script>props 主要定义当前封装自己新增的能力。attrs 通常继续透传给底层 element-plus 组件。v-model,一般会同时保留 update:modelValue 和业务事件。<el-select
v-bind="{
clearable: true,
filterable: true,
...Object.entries($attrs).reduce((obj, [key, value]) => {
if (key !== 'class' && key !== 'style') {
obj[key] = value
}
return obj
}, {}),
}"
/><el-select
v-bind="{
clearable: true,
filterable: true,
...Object.entries($attrs).reduce((obj, [key, value]) => {
if (key !== 'class' && key !== 'style') {
obj[key] = value
}
return obj
}, {}),
}"
/>class 和 style 往往会单独处理,避免覆盖组件自身样式。<template v-for="(arg, name, index) in noDefaultSlots" v-slot:[name]>
<slot :name="name" v-bind="arg" :index="index" />
</template><template v-for="(arg, name, index) in noDefaultSlots" v-slot:[name]>
<slot :name="name" v-bind="arg" :index="index" />
</template>const noDefaultSlots = computed(() => {
const copySlots = proxy.clone(slots)
delete copySlots.default
delete copySlots.label
return copySlots
})const noDefaultSlots = computed(() => {
const copySlots = proxy.clone(slots)
delete copySlots.default
delete copySlots.label
return copySlots
})el-select、el-table 一类本身插槽很多的组件。withInstall import Foo from './src/index.vue'
import { withInstall } from '@/components/utils/withInstall.ts'
const SFoo = withInstall(Foo)
export default SFooimport Foo from './src/index.vue'
import { withInstall } from '@/components/utils/withInstall.ts'
const SFoo = withInstall(Foo)
export default SFooindex.ts 专门做这件事。组件全局默认值直接写在 app.use 的第二个参数里,风格和 Element Plus 保持一致。
// 组件内部统一走 hook
const mergedProps = useGlobalComponentConfig('select', props)// 组件内部统一走 hook
const mergedProps = useGlobalComponentConfig('select', props)// main.ts 中全局传入
const app = createApp(App)
app.use(SybzComponents, {
theme: 'chenghua',
size: 'small',
select: {
showPrefix: true,
},
table: {
showIndex: false,
showPage: false,
pageSizes: [10, 30, 50, 100],
},
})// main.ts 中全局传入
const app = createApp(App)
app.use(SybzComponents, {
theme: 'chenghua',
size: 'small',
select: {
showPrefix: true,
},
table: {
showIndex: false,
showPage: false,
pageSizes: [10, 30, 50, 100],
},
})showPrefix、showQuick 一类开关。theme、size 这类和组件平级的字段会作为公共默认值,分发给声明了同名 prop 且接入了 useGlobalComponentConfig 的组件。select、table 这类组件名字段只对对应组件生效,可用于覆盖公共默认值或补充组件自己的默认行为。useGlobalComponentConfig('组件key', props)。setup 里继续使用 provide('GLOBAL_COMPONENT_CONFIG', { select: { ... } }) 做局部覆盖。home.md + 多个 demo.vue ### 基础用法
:::demo
foo/base
:::### 基础用法
:::demo
foo/base
:::home.md 负责目录、说明、属性表和事件表。.vue 文件,避免一个文档页过长。## Hidden Title {.md-hidden} 这一行,和当前仓库风格保持一致。packages/utils/src/base.ts
packages/utils/src/index.ts
docs/components/utils/isEmpty/home.mdpackages/utils/src/base.ts
packages/utils/src/index.ts
docs/components/utils/isEmpty/home.mdpackages/utils/src/* 下。packages/utils/src/index.ts 导出。docs/components/utils/*/home.md,路径尽量和函数名保持一致。docs/.vitepress/config.ts 里是否补了侧边栏入口。index.ts,这样才能被 packages/index.ts 自动收集。docs/components/xxx/home.md 是否已经引用了对应示例。pnpm build;如果本次改动涉及文档结构,也建议补跑 pnpm docsbuild。