Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mall-webview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontend
mall-webview
Commits
2c95129f
Commit
2c95129f
authored
Mar 29, 2021
by
hewenhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: native bridge 支持监听多个事件
parent
ab937710
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
6 deletions
+27
-6
use-native-bridge.ts
src/util/use-native-bridge.ts
+19
-3
index.vue
src/views/test/index.vue
+8
-3
No files found.
src/util/use-native-bridge.ts
View file @
2c95129f
...
@@ -37,14 +37,30 @@ const sendNativeMessage = (data: { type: string; data: any }) => {
...
@@ -37,14 +37,30 @@ const sendNativeMessage = (data: { type: string; data: any }) => {
window
.
ReactNativeWebView
.
postMessage
(
JSON
.
stringify
(
data
));
window
.
ReactNativeWebView
.
postMessage
(
JSON
.
stringify
(
data
));
};
};
type
ListenerType
=
(
this
:
ComponentInternalInstance
|
null
,
...
arg
:
any
[])
=>
any
;
function
useNativeBridge
(
function
useNativeBridge
(
eventType
:
string
,
eventType
:
string
,
listener
:
(
this
:
ComponentInternalInstance
|
null
,
...
arg
:
any
[])
=>
any
listener
:
ListenerType
):
[
typeof
sendNativeMessage
,
typeof
eventBus
];
function
useNativeBridge
(
eventMap
:
{
[
key
:
string
]:
ListenerType
;
}):
[
typeof
sendNativeMessage
,
typeof
eventBus
];
function
useNativeBridge
(
eventTypeOrEventMap
:
string
|
{
[
key
:
string
]:
ListenerType
},
listener
?:
ListenerType
):
[
typeof
sendNativeMessage
,
typeof
eventBus
]
{
):
[
typeof
sendNativeMessage
,
typeof
eventBus
]
{
const
_listener
=
function
(
this
:
ComponentInternalInstance
|
null
,
arg
?:
NativeInvokeFuncParams
)
{
const
_listener
=
function
(
this
:
ComponentInternalInstance
|
null
,
arg
?:
NativeInvokeFuncParams
)
{
if
(
!
arg
)
return
;
if
(
!
arg
)
return
;
if
(
eventType
===
arg
.
method
)
{
const
{
method
,
data
}
=
arg
;
listener
.
apply
(
this
,
[
arg
.
data
]);
if
(
typeof
eventTypeOrEventMap
===
"string"
)
{
if
(
eventTypeOrEventMap
===
method
&&
listener
)
{
listener
.
apply
(
this
,
[
data
]);
}
}
else
if
(
typeof
eventTypeOrEventMap
[
method
]
===
"function"
)
{
eventTypeOrEventMap
[
method
].
apply
(
this
,
[
data
]);
}
}
};
};
...
...
src/views/test/index.vue
View file @
2c95129f
...
@@ -7,13 +7,18 @@
...
@@ -7,13 +7,18 @@
import
{
ref
}
from
"vue"
;
import
{
ref
}
from
"vue"
;
import
{
useNativeBridge
}
from
"@/util/use-native-bridge"
;
import
{
useNativeBridge
}
from
"@/util/use-native-bridge"
;
const
[
sendNativeMessage
,
eventBus
]
=
useNativeBridge
(
"bindWechatChange"
,
function
(
arg
)
{
const
[
sendNativeMessage
,
eventBus
]
=
useNativeBridge
({
console
.
log
(
this
,
arg
);
onBindWechatChange
(
arg
)
{
console
.
log
(
this
,
arg
);
}
});
useNativeBridge
(
"onBindWechatChange"
,
function
(
arg
)
{
console
.
log
(
this
,
arg
,
"bb"
);
});
});
const
count
=
ref
(
1000
);
const
count
=
ref
(
1000
);
const
emitTest
=
()
=>
{
const
emitTest
=
()
=>
{
eventBus
.
emit
(
"GEEK_NATVIE_NAME"
,
{
eventBus
.
emit
(
"GEEK_NATVIE_NAME"
,
{
method
:
"
b
indWechatChange"
,
method
:
"
onB
indWechatChange"
,
data
:
{
data
:
{
hhh
:
13213
hhh
:
13213
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment