面临的问题:一个公共控件需要用到一个方法,这个方法不固定,不同父控件有自己独立的方法,并且这个方法有一样的传参,及接收结构。
解决:如果外部提供方法给这个公共控件就可以解决这个问题。
公共控件inject:
<script setup lang="ts">
import { inject, ref, watch } from "vue";
const getAttachmentAuth = inject("getAttachmentAuth") as (
d: any,
p: any
) => { Code; Data; Message };
const onDownload = async (row: BidAttachmentInfo) => {
try {
const { Code, Data, Message } = await getAttachmentAuth(
row.SerialNum,
row.AttachGuid
);
if (Code === 0 && Data === true) {
downloadAttach(row.AttachGuid, row.AttachName, row.Extension);
} else {
errorMessage(Message || "无权下载");
}
} catch (err) {
errorMessage(err);
}
};
</script>
父控件提供方法:
<script setup lang="ts">
export const getAttachmentAuth = (FormSn: string, fileId: string): resType => {
return http.get("BidModuleList/DownloadAttachAuth", {
FormSn: FormSn,
fileId: fileId
});
};
provide("getAttachmentAuth", getAttachmentAuth);
</script>