Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望增加一个配置不需要在每个页面都定义uniLayout的ref #41

Open
2 tasks done
Shigukj opened this issue May 26, 2024 · 5 comments
Open
2 tasks done
Labels
enhancement New feature or request

Comments

@Shigukj
Copy link

Shigukj commented May 26, 2024

对问题的清晰和简明的描述

我在layouts写了公共组件并且defineExpose 抛出。我要通过PageInstance获取每个页面的方法

推荐的解决方案

在配置中增加是否自动加入ref,而不是需要每个页面添加const uniLayout = ref()

替代方案

No response

额外上下文

No response

检查

  • 遵循我们的 行为准则
  • 检查是否已经有一个要求相同功能的问题,以避免重复创建。
@Skiyee
Copy link
Member

Skiyee commented May 27, 2024

可以结合例子说明一下?没能理解其中的关联?

@Shigukj
Copy link
Author

Shigukj commented May 29, 2024

layout.vue

<script setup>
const toastRef = ref()
defineExpose({
  toastRef
})
</script>
<template>
<view class="page-app">
 <toast ref="toastRef" />
 </view>
</template>

utils.js

/**
 * @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

@Skiyee
Copy link
Member

Skiyee commented Jun 9, 2024

@ModyQyW 大哥,看看这个issue

@Skiyee
Copy link
Member

Skiyee commented Jun 9, 2024

在一个页面内 const uniLayout = ref() 确实有点奇怪,看不出uniLayout是被谁引用,有点不符合直觉

咱能否放一个option: enableLayoutRef: boolean

@ModyQyW ModyQyW added the enhancement New feature or request label Jun 11, 2024
@Skiyee
Copy link
Member

Skiyee commented Aug 12, 2024

由于团队各成员时间问题,欢迎各位朋友提交该与功能相关的PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants