Open Shigukj opened 4 months ago
可以结合例子说明一下?没能理解其中的关联?
layout.vue
<script setup> const toastRef = ref() defineExpose({ toastRef }) </script> <template> <view class="page-app"> <toast ref="toastRef" /> </view> </template>
> utils.js
```vue
/**
* @description 显示消息提示框
* @param {string} title 提示的内容,长度与 icon 取值有关。
* @param {number} duration 提示的延迟时间,单位毫秒,默认:2000
*/
export function toast(title: string, type?: 'none' | 'success' | 'loading' | 'error' | 'fail' | 'exception' | 'warning' | undefined, duration = 1000, complete?: AnyFn) {
try {
let hasIcon = true
if (type === undefined || type === 'none')
hasIcon = false
const pages = pagesInstance()
if (pages.length > 0) {
pages[pages.length - 1].$vm.$refs.uniLayout.toastRef.show({
type,
icon: hasIcon,
message: title,
duration,
complete,
})
}
}
catch (err) {
if (type === 'warning')
type = 'none'
uni.showToast({
title: String(title),
icon: type || 'none',
duration,
})
}
}
通过refs调取每个页面上的toast组件,我就要在每个页面上写 const uniLayout = ref() ,不然获取不到 uniLayout
@ModyQyW 大哥,看看这个issue
在一个页面内 const uniLayout = ref()
确实有点奇怪,看不出uniLayout
是被谁引用,有点不符合直觉
咱能否放一个option: enableLayoutRef: boolean
由于团队各成员时间问题,欢迎各位朋友提交该与功能相关的PR
对问题的清晰和简明的描述
我在layouts写了公共组件并且defineExpose 抛出。我要通过PageInstance获取每个页面的方法
推荐的解决方案
在配置中增加是否自动加入ref,而不是需要每个页面添加const uniLayout = ref()
替代方案
No response
额外上下文
No response
检查