Closed kkfive closed 11 months ago
provide/inject
// useToast.ts
const body = ref(null);
export function useToastBody() {
return body;
}
export function useToast() { function add(_body) { body.value = _body; // your code... } function remove() { body.value = null; // your code... } return { add, remove, success: (body) => add({ type: "success", ...body }), error: (body) => add({ type: "error", ...body }), // more type alias... }; }
```vue
<!-- layouts/default.vue -->
<script setup>
const body = useToastBody();
</script>
<template>
<div>
<Toast :body="body" />
<slot />
</div>
</template>
<!-- pages/index.vue -->
<script setup>
const toast = useToast();
toast.add({
// your code...
});
</script>
this.$refs.layout
来访问 layout 中 defineExpose
提供的属性目前 DX 体验最好的应该是第二种方法,最方便的应该是第三种方法
目前我通过在layout中把方法挂在给uni对象 可以实现
但是我觉得可以尝试下第二种
provide/inject
这个我尝试了下 好像不太行
应该是可以的,正如 readme 所说的,layouts 插件不是魔法 :)
描述问题
例如 在模板中定义一个 toast 组件,并将show方法暴露给子组件来调用
复现
系统信息
使用的包管理器
pnpm
核对