Skip to content

hooks Composition API

useShare Mini Program Global Share Configuration

useShare(options?) =>

Configures sharing globally for the mini program; returns share methods to be exposed on pages.

Parameters

Parameter NameTypeRequiredDefaultDescription
optionsobjectNo-Share configuration
options.titlestringNo-Title name
options.pathstringNo-Mini program path
options.friendImageUrlstringNo-Cover image for sharing with friends
options.timelineImageUrlstringNo-Cover image for sharing to Moments

Return Value

TypeDescription
objectContains the onShareAppMessage and onShareTimeline methods

Example

  1. Single page usage When a page needs to customize its share content, register it in <script setup> together with the hooks from @dcloudio/uni-app:
Index.vue
typescript
<script setup lang="ts">
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { useShare } from '@hy-app/ui'

const share = useShare({
  title: '页面标题',
  path: '/pages/index/index',
  friendImageUrl: '/static/share-friend.png',
  timelineImageUrl: '/static/share-timeline.png'
})

// 注册到页面级,优先于全局 mixin 的默认分享
onShareAppMessage(share.onShareAppMessage)
onShareTimeline(share.onShareTimeline)
</script>
  1. Global usage

After global registration, the "Forward to Friend / Share to Moments" options in the WeChat mini program's top-right menu become available, without needing to write them separately for each page.

main.ts
typescript
import { useShare } from '@hy-app/ui';

app.mixin(
  useShare({
    title: '华玥组件库'
  })
)

useToast Global Toast Messages

useToast() => ToastInstance

A global toast message composable API that provides multiple types of message prompts.

Return Value

TypeDescription
ToastInstanceToast instance, containing the show, info, success, error, warning, primary, loading, and close methods

ToastInstance Methods

MethodParametersDescription
showmessage, options?Default toast
infomessage, options?Info toast
successmessage, options?Success toast
errormessage, options?Error toast
warningmessage, options?Warning toast
primarymessage, options?Primary toast
loadingmessage?, options?Loading
close-Close all toasts

options Parameters

ParameterTypeRequiredDefaultDescription
messagestringYes-Text message to display
typestringNo-Theme type: primary, success, error, warning, info
positionstringNo-Position where the toast appears: top, center, bottom
iconboolean | stringNo-Icon to display
overlaybooleanNo-Whether to prevent touch pass-through
durationnumberNo-Duration (in milliseconds)

Example

typescript
import { useToast } from '@hy-app/ui';

const toast = useToast();

toast.success('操作成功!');
toast.error('操作失败');
toast.loading('加载中...');

useMessage Message Dialog Composable API

useMessage() => MessageInstance

A message dialog composable API that provides alert and confirm methods.

Return Value

TypeDescription
MessageInstanceMessage instance, containing the alert and confirm methods

MessageInstance Methods

MethodParametersDescription
alertmessage | optionsAlert dialog
confirmmessage | optionsConfirm dialog

options Parameters

ParameterTypeRequiredDefaultDescription
titlestringNo-Dialog title
contentstringYes-Dialog content
confirmTextstringNo-Confirm button text
cancelTextstringNo-Cancel button text
showConfirmButtonbooleanNo-Whether to show the confirm button
showCancelButtonbooleanNo-Whether to show the cancel button
confirmColorstringNo-Confirm button color
cancelColorstringNo-Cancel button color
confirmfunctionNo-Confirm button click callback
cancelfunctionNo-Cancel button click callback

Example

typescript
import { useMessage } from '@hy-app/ui';

const message = useMessage();

const result = await message.confirm({
    title: '删除确认',
    content: '确定要删除吗?',
    confirmText: '确定',
    cancelText: '取消',
});

useTouch Touch Event Composable API

useTouch() => TouchInstance

A touch event composable API used to track and analyze user touch operations.

Return Value

TypeDescription
TouchInstanceTouch instance

TouchInstance Properties and Methods

NameTypeDescription
touchStartfunctionTouch start handler
touchMovefunctionTouch move handler
directionrefTouch direction
deltaXrefHorizontal displacement
deltaYrefVertical displacement
offsetXrefHorizontal offset
offsetYrefVertical offset

Example

typescript
import { useTouch } from '@hy-app/ui';

const { touchStart, touchMove, direction, deltaX, deltaY } = useTouch();

useShakeService Shake Sensor Composable API

useShakeService(threshold?) =>

A shake sensor composable API that implements shake detection by listening to device acceleration.

Parameters

ParameterTypeRequiredDefaultDescription
thresholdnumberNo-Shake threshold

Return Value

TypeDescription
objectContains the startShakeListener and stopShakeListener methods

Example

typescript
import { useShakeService } from '@hy-app/ui';

const { startShakeListener, stopShakeListener } = useShakeService();

startShakeListener(() => {
    console.log('摇一摇触发');
});

useTranslate Internationalization Composable API

useTranslate(module?) =>

An internationalization translation composable API used to implement multi-language switching in components or pages.

Parameters

ParameterTypeRequiredDefaultDescription
modulestringNo-Language pack module name

Return Value

TypeDescription
objectContains the t translation method

Example

typescript
import { useTranslate } from '@hy-app/ui';

const { t } = useTranslate('common');

console.log(t('hello'));
console.log(t('welcome', '华玥'));

useQueue Queue Management Composable API

useQueue() => QueueInstance

A queue management composable API used to manage component display order and mutually exclusive closing logic.

Return Value

TypeDescription
QueueInstanceQueue instance

QueueInstance Methods

MethodParametersDescription
pushToQueuecomponentAdd a component to the queue
removeFromQueuecomponentRemove a component from the queue
closeOthercomponentClose all components except the current one
closeOutside-Close all components

Example

typescript
import { useQueue } from '@hy-app/ui';

const { pushToQueue, removeFromQueue, closeOther } = useQueue();

pushToQueue(popupRef.value);
closeOther(popupRef.value);