+-
如何在 react-native-webview 中向通过 injectJavaScript 调用的函数传递动态 param。

我正在使用react-native-webview来加载webview中的url。我只是想使用webview从原生应用中调用前端的一个函数来实现一些功能。

this.webView.ref.injectJavaScript('window.chatComponentService.openChat()')

使用静态参数可以正常工作

this.webView.ref.injectJavaScript('window.chatComponentService.openChat("anyValue")')

但是,如果我想传递动态值,比如。

this.webView.ref.injectJavaScript('window.chatComponentService.openChat(&{someParam})')

我应该怎么做才能在函数调用中传递动态参数?

0
投票

你可以在发送之前对params进行JSON.string化。我通常做的是

const message = JSON.stringify({
    x: 'value1',
    y: 'value2',
    z: 'value3'
});

this.webView.ref.injectJavaScript(`window.chatComponentService.openChat(${message})`)