fix(page-header): backIcon 插槽在html 文件中无效
Signed-off-by: forget skyrim <11513667+forget-skyrim@user.noreply.gitee.com>
This commit is contained in:
parent
38b2941990
commit
b22e4cb098
@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<div class="lay-page-header">
|
||||
<div class="lay-page-header__left" @click="emits('back')">
|
||||
<slot name="backIcon"
|
||||
<slot :name="backIconSlotName"
|
||||
><i class="layui-icon" :class="[backIcon]"></i
|
||||
></slot>
|
||||
<div class="lay-page-header__title">{{ backText }}</div>
|
||||
@ -19,7 +19,8 @@ export default {
|
||||
};
|
||||
</script>
|
||||
<script lang="ts" setup>
|
||||
import { useSlots } from "vue";
|
||||
import { convertSlotName } from "../../utils";
|
||||
import { getCurrentInstance, useSlots } from "vue";
|
||||
import "./index.less";
|
||||
|
||||
export interface PageHeaderProps {
|
||||
@ -34,6 +35,7 @@ const props = withDefaults(defineProps<PageHeaderProps>(), {
|
||||
});
|
||||
|
||||
const emits = defineEmits(["back"]);
|
||||
|
||||
const slots = useSlots();
|
||||
const instance = getCurrentInstance()!;
|
||||
const backIconSlotName = convertSlotName(instance, "backIcon");
|
||||
</script>
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Component, VNode, VNodeTypes } from "vue";
|
||||
import { Component, ComponentInternalInstance, VNode, VNodeTypes } from "vue";
|
||||
|
||||
export enum ShapeFlags {
|
||||
ELEMENT = 1,
|
||||
@ -31,3 +31,27 @@ export const isArrayChildren = (
|
||||
): children is VNode[] => {
|
||||
return Boolean(vn && vn.shapeFlag & ShapeFlags.ARRAY_CHILDREN);
|
||||
};
|
||||
|
||||
/**
|
||||
* 同时支持驼峰命名和破折号命名的插槽,示例:back-icon 和 backIcon
|
||||
* @param vm 组件实例
|
||||
* @param name 插槽名
|
||||
*/
|
||||
export function convertSlotName(vm: ComponentInternalInstance, name: string) {
|
||||
const camelCaseName = camelCase(name);
|
||||
const kebabCaseName = kebabCase(name);
|
||||
return vm.slots[camelCaseName]
|
||||
? camelCaseName
|
||||
: vm.slots[kebabCaseName]
|
||||
? kebabCaseName
|
||||
: name
|
||||
}
|
||||
|
||||
export function camelCase(str: string) {
|
||||
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
|
||||
}
|
||||
|
||||
export function kebabCase(key: string) {
|
||||
const result = key.replace(/([A-Z])/g, ' $1').trim();
|
||||
return result.split(' ').join('-').toLowerCase()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user