File tree 6 files changed +121
-0
lines changed
hooks/src/useLaunchOptions
6 files changed +121
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ export default {
20
20
// wechat
21
21
'pages/useAPICheck/index' ,
22
22
'pages/useUpdateManager/index' ,
23
+ 'pages/useLaunchOptions/index' ,
23
24
// network
24
25
'pages/useRequest/index' ,
25
26
'pages/useRequest/defaultRequest/index' ,
Original file line number Diff line number Diff line change @@ -88,6 +88,10 @@ export const ChildrenList: { [_: string]: APIChildrenItem[] } = {
88
88
id : 'useUpdateManager' ,
89
89
name : 'useUpdateManager 更新' ,
90
90
} ,
91
+ {
92
+ id : 'useLaunchOptions' ,
93
+ name : 'useLaunchOptions 启动参数' ,
94
+ } ,
91
95
] ,
92
96
[ APIChildrenName . network ] : [
93
97
{
@@ -104,3 +108,12 @@ export const ChildrenList: { [_: string]: APIChildrenItem[] } = {
104
108
} ,
105
109
] ,
106
110
} ;
111
+
112
+ export const SceneEnum : { [ _ : number ] : string } = {
113
+ 1020 : '公众号 profile 页相关小程序列表' ,
114
+ 1035 : '公众号自定义菜单' ,
115
+ 1036 : 'App 分享消息卡片' ,
116
+ 1037 : '小程序打开小程序' ,
117
+ 1038 : '从另一个小程序返回' ,
118
+ 1043 : '公众号模板消息' ,
119
+ } ;
Original file line number Diff line number Diff line change
1
+ export default {
2
+ navigationBarTitleText : 'useLaunchOptions' ,
3
+ } ;
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+ import { AtRadio , AtNoticebar } from 'taro-ui' ;
3
+ import DocPage from '@components/DocPage' ;
4
+
5
+ import { General , ENV_TYPE } from '@tarojs/taro' ;
6
+ import { useLaunchOptions , useEnv } from 'taro-hooks' ;
7
+
8
+ import 'taro-ui/dist/style/components/icon.scss' ;
9
+
10
+ import { SceneEnum } from '../../constant' ;
11
+
12
+ const MOCK = '1' ;
13
+
14
+ const transferOptions = ( launchOptions : General . LaunchOptionsApp ) =>
15
+ Object . keys ( launchOptions ) . map ( ( key : any ) => {
16
+ let desc = JSON . stringify (
17
+ launchOptions [ key as keyof General . LaunchOptionsApp ] || '' ,
18
+ ) ;
19
+
20
+ if ( key === 'scene' ) {
21
+ desc =
22
+ desc +
23
+ ':' +
24
+ ( SceneEnum [ launchOptions [ key as keyof General . LaunchOptionsApp ] ] || '' ) ;
25
+ }
26
+ return {
27
+ label : key + ':' ,
28
+ value : key ,
29
+ desc,
30
+ } ;
31
+ } ) ;
32
+
33
+ export default ( ) => {
34
+ const launchOptions = useLaunchOptions ( ) ;
35
+ const env = useEnv ( ) ;
36
+ return (
37
+ < >
38
+ < AtNoticebar marquee > 请尝试从不同场景打开小程序观察参数变化</ AtNoticebar >
39
+ < DocPage title = "useLaunchOptions 启动参数" panelTitle = "useLaunchOptions" >
40
+ { env !== ENV_TYPE . WEAPP && '改hook仅可在小程序中使用' }
41
+ < AtRadio
42
+ options = { transferOptions ( launchOptions ) }
43
+ value = { MOCK }
44
+ onClick = { console . log }
45
+ />
46
+ </ DocPage >
47
+ </ >
48
+ ) ;
49
+ } ;
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : useLaunchOptions
3
+ nav :
4
+ title : Hooks
5
+ path : /hooks
6
+ order : 2
7
+ group :
8
+ title : 小程序
9
+ path : /wechat
10
+ ---
11
+
12
+ # useLaunchOptions
13
+
14
+ 获取小程序启动时参数(仅小程序端可用)
15
+
16
+ ## 何时使用
17
+
18
+ 当要对小程序启动时做参数判断时
19
+
20
+ ## API
21
+
22
+ ``` jsx | pure
23
+ const launchOptions = useLaunchOptions ();
24
+ ```
25
+
26
+ ## 返回值说明
27
+
28
+ | 返回值 | 说明 | 类型 |
29
+ | ------------- | -------- | ------------------ |
30
+ | launchOptions | 启动参数 | ` LaunchAppOptions ` |
31
+
32
+ ## 代码演示
33
+
34
+ <code src =" @pages/useLaunchOptions " />
35
+
36
+ ## Hook 支持度
37
+
38
+ | 微信小程序 | H5 | ReactNative |
39
+ | :--------: | :-: | :---------: |
40
+ | ✔️ | | |
41
+
42
+ ## FAQ
43
+
44
+ ### 1. 更多说明
45
+
46
+ 见[ 小程序相关文档] ( https://developers.weixin.qq.com/miniprogram/dev/api/base/app/life-cycle/wx.getLaunchOptionsSync.html )
Original file line number Diff line number Diff line change
1
+ import { ENV_TYPE , getLaunchOptionsSync , General } from '@tarojs/taro' ;
2
+ import useEnv from '../useEnv' ;
3
+
4
+ function useLaunchOptions ( ) : General . LaunchOptionsApp | { } {
5
+ const env = useEnv ( ) ;
6
+ return env === ENV_TYPE . WEAPP ? getLaunchOptionsSync ( ) : { } ;
7
+ }
8
+
9
+ export default useLaunchOptions ;
You can’t perform that action at this time.
0 commit comments