<template>
<div class="y-collapse"><slot /></div>
</template>
<script setup lang="ts">
import { provide } from "vue";
import { collapseContextKey } from "./types";
import type { CollapseProps, NameType } from "./types";
defineOptions({ name: "YCollapse", inheritAttrs: false });
const props = defineProps<CollapseProps>();
const activeNames = defineModel<NameType[]>();
if (props.accordion && activeNames.value.length > 1) {
console.warn("accordion mode should only have one active item");
}
const handleItemClick = (item: NameType) => {
if (props.accordion) {
activeNames.value = [activeNames.value[0] === item ? "" : item];
} else {
const index = activeNames.value.indexOf(item);
if (index > -1) {
activeNames.value.splice(index, 1);
} else {
activeNames.value.push(item);
}
}
};
provide(collapseContextKey, {
activeNames,
handleItemClick,
});
</script>
老师我改成这样后,activeNames报错了,
provide那报错Type ‘ModelRef<NameType[] | undefined, string>’ is not assignable to type ‘Ref<NameType[]>’.
Types of property ‘value’ are incompatible.
Type ‘NameType[] | undefined’ is not assignable to type ‘NameType[]’.
Type ‘undefined’ is not assignable to type ‘NameType[]’.ts-plugin(2322)
types.ts(16, 3): The expected type comes from property ‘activeNames’ which is declared here on type ‘CollapseContext’
其它地方报错’activeNames.value’ is possibly ‘undefined’.
老师能麻烦你给一个全一点的代码吗